主頁 >  其他 > 背包問題——無限物品的背包問題,0-1背包問題與勁歌金曲(uva12563)

背包問題——無限物品的背包問題,0-1背包問題與勁歌金曲(uva12563)

2021-05-04 18:09:28 其他

無限物品的背包問題

物品無限的背包問題,有n種物品,每種均有無窮多個,第i種物品的體積為Vi,重量為Wi,選一些物品裝到一個容量為C的背包中,使得背包內物品在總體積不超過C的前提下重量盡量大,1≤n≤1001≤n≤100,1≤Vi≤C≤10000,1≤Wi≤106,

測驗資料:
3 5
1 2
2 3
3 2
answer:10

3 7
2 1
3 2
4 3
answer:5

3 5
3 3
4 2
3 2
answer:3

0

解題思路

dp思維:
每個物品都可以無限使用,且我們不需要考慮能否完全利用背包的容量C,只要所裝物品的重量總和最大即可,由于物品是無限使用的,所以在狀態轉移中我們所需要思考的是此時背包的容量,而不是對于面向物品思考狀態,
狀態轉移方程:
i代表著背包的容積
dp【i】=max(dp【i】,dp【i-v【j】】+w【j】)

遞回思維:
相當于一個有向圖的動態規劃,我們對每個重量都探討它接下可裝物品的最大重量,

遞回:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 103
#define M 100003
#define Max(a,b) ((a)>(b)?(a):(b))
//由于物品無限且不需要如硬幣問題一樣必須裝的恰到好處
int d[M],C;
int v[N],w[N];
int dp(int C,int n)
{
   int *ans=&d[C],i;   //ans改變d【c】改變
   if(*ans>0) return *ans;
   *ans=0;
   for(i=0;i<n;i++)
    if(C>=v[i])
      *ans=Max(*ans,dp(C-v[i],n)+w[i]);
    return *ans;
}

int main()
{
   int n,ans,i;
   while(~scanf("%d%d",&n,&C))
   {
       for(i=0;i<n;i++)
        scanf("%d%d",&v[i],&w[i]);
       memset(d,0,sizeof(int)*M);   //初始化
       ans=dp(C,n);
       printf("ans:%d\n",ans);
   }
   return 0;
}

遞推:

#include <stdio.h>
#include <stdlib.h>
#define N 103
#define M 100003
#define Max(a,b) ((a)>(b)?(a):(b))

int main()
{
    int C,n;
    while(~scanf("%d%d",&n,&C))
    {
        int i,j;
        int v[n],w[n];
        int dp[M]={0};  //初始化為0

        for(i=0;i<n;i++)
            scanf("%d%d",&v[i],&w[i]);

        for(i=1;i<=C;i++)
            for(j=0;j<n;j++)
            if(i>=v[j])
                dp[i]=Max(dp[i],dp[i-v[j]]+w[j]);
        printf("ans:%d\n",dp[C]);
    }
    return 0;
}

01背包問題

有n種物品,每種均只有1個,第i種物品的體積為Vi,重量為Wi,選一些物品裝到一個容量為CC的背包中,使得背包內物品在總體積不超過C的前提下重量盡量大,1≤n≤100,1≤Vi≤C≤10000,1≤Wi≤106,

測驗資料:
5 6             
1 2
2 4
3 4
4 5 
5 6
answer:10

解題思路

遞回

由于每個物品只能使用一次,所以我們需要標記,類似于DFS進行標記與恢復標記,再利用圖的思維,對著不同體積,探討它接下來體積所能裝下物品重量的最大值

#include <stdio.h>
#include <stdlib.h>
#define N 103
#define M 100003
#define Max(a,b) ((a)>(b)?(a):(b))

int flag[N];   //物品只能用一次需要標記類似dfs
int n,v[N],w[N];
int dp(int C)   //為什么不加入記憶化?由于標記的存在并不適合記憶化.
{
   int ans=0,i;
   for(i=0;i<n;i++)
     if(!flag[i] && C>=v[i])
     {
         flag[i]=1;
         ans=Max(ans,dp(C-v[i])+w[i]);
         flag[i]=0; //恢復狀態
     }
   return ans;
}

int main()
{
    int C;
    while(~scanf("%d%d",&n,&C))
    {
        int i;
        for(i=0;i<n;i++)
            scanf("%d%d",&v[i],&w[i]);
        printf("ans:%d\n",dp(C));
    }
    return 0;
}

遞推

二維打表思維*
這回是由于物品只能思考一個,所以我們所要面對思考的物件是物品,而不是此時背包的容量,為什么不面向背包的容量進行思考呢?當你面向背包進行思考時,你就需要加入一個標記,而既然時遞推,你就無法像上面那樣進行多次嘗試,也就是說是遞推限制了我們面向背包容量的思考,
我們要對每個物品需要考慮:裝或者不裝,那么我們的狀態轉移方程就將由此產生,裝下去大呢還是不裝下去大呢
總結:這是把前i個物品裝入背包中的最大重量和的狀態轉移
狀態轉移方程:
dp【i】【j】=max(dp【i-1】【j】,dp【i-1】【j-v【i】】+w【i】) (i代表物品數,j代表重量)

這里我們用的是自底向上的思維

#include <stdio.h>
#include <stdlib.h>
#define N 103
#define M 100003
#define Max(a,b) ((a)>(b)?(a):(b))

int dp[N][M];
int main()
{
    int C,n,i,j,v[N],w[N];
    while(~scanf("%d%d",&n,&C))
    {
        for(i=1;i<=n;i++)      //由于要預留dp【0】的位置
            scanf("%d%d",&v[i],&w[i]);

        for(i=0;i<N;i++)
            for(j=0;j<M;j++)
            dp[i][j]=0;

        for(i=1;i<=n;i++)
            for(j=1;j<=C;j++)
         if(j>=v[i])
            dp[i][j]=Max(dp[i-1][j],dp[i-1][j-v[i]]+w[i]);
          else
            dp[i][j]=dp[i-1][j];
        printf("%d\n",dp[n][C]);
    }
    return 0;
}

我們發現每次dp只需要用到上一層的陣列那么我們可以對dp陣列進行優化,變成一維,
注意:這里對容量C我們需要自頂向下,不然會出現dp改變了前面的數,dp【j-v【i】】+w【i】中的dp【j-v【i】】用的是改變后的數,不是我們所需要的保存上一層陣列的數,

#include <stdio.h>
#include <stdlib.h>
#define N 103
#define M 100003
#define Max(a,b) ((a)>(b)?(a):(b))


int main()
{
    int n,C,i,j;
    while(~scanf("%d%d",&n,&C))
    {
        int dp[M]={0};
        int v[N],w[N];
        for(i=0;i<n;i++)
            scanf("%d%d",&v[i],&w[i]);
        for(i=0;i<n;i++)
            for(j=C;j>=v[i];j--)   //這里要自頂向下不然會出現dp【i】改變了前面的數導致后面的j-v【i】用的是改變后的數導致結果例外
                dp[j]=Max(dp[j],dp[j-v[i]]+w[i]);
        printf("ans%d\n",dp[C]);
    }
    return 0;
}

還可再進行一點簡單的優化:

 for(i=0;i<n;i++){
     scanf("%d%d",&v,&w);
     for(j=C;j>=v;j--)  
             dp[j]=Max(dp[j],dp[j-v]+w);
  }

勁歌金曲

(If you smiled when you see the title, this problem is for you _)
For those who don’t know KTV, see: https://en.wikipedia.org/wiki/Karaoke_box
There is one very popular song called Jin Ge Jin Qu(勁歌金曲). It is a mix of 37 songs, and is
extremely long (11 minutes and 18 seconds) — I know that there are Jin Ge Jin Qu II and III, and
some other unofficial versions. But in this problem please forget about them.
Why is it popular? Suppose you have only 15 seconds left (until your time is up), then you should
select another song as soon as possible, because the KTV will not crudely stop a song before it ends
(people will get frustrated if it does so!). If you select a 2-minute song, you actually get 105 extra
seconds! ….and if you select Jin Ge Jin Qu, you’ll get 663 extra seconds!!!
Now that you still have some time, but you’d like to make a plan now. You should stick to the
following rules:
? Don’t sing a song more than once (including Jin Ge Jin Qu).
? For each song of length t, either sing it for exactly t seconds, or don’t sing it at all.
? When a song is finished, always immediately start a new song.
Your goal is simple: sing as many songs as possible, and leave KTV as late as possible (since we
have rule 3, this also maximizes the total lengths of all songs we sing) when there are ties.
Input
The first line contains the number of test cases T (T ≤ 100). Each test case begins with two positive
integers n, t (1 ≤ n ≤ 50, 1 ≤ t ≤ 109
), the number of candidate songs (BESIDES Jin Ge Jin Qu)
and the time left (in seconds). The next line contains n positive integers, the lengths of each song, in
seconds. Each length will be less than 3 minutes — I know that most songs are longer than 3 minutes.
But don’t forget that we could manually “cut” the song after we feel satisfied, before the song ends. So
here “length” actually means “length of the part that we want to sing”.
It is guaranteed that the sum of lengths of all songs (including Jin Ge Jin Qu) will be strictly larger
than t.
Output
For each test case, print the maximum number of songs (including Jin Ge Jin Qu), and the total lengths
of songs that you’ll sing.
Explanation:
In the first example, the best we can do is to sing the third song (80 seconds), then Jin Ge Jin Qu
for another 678 seconds.
In the second example, we sing the first two (30+69=99 seconds). Then we still have one second
left, so we can sing Jin Ge Jin Qu for extra 678 seconds. However, if we sing the first and third song
instead (30+70=100 seconds), the time is already up (since we only have 100 seconds in total), so we
can’t sing Jin Ge Jin Qu anymore!Universidad de Valladolid OJ: 12563 – Jin Ge Jin Qu hao 2/2
Sample Input
2
3 100
60 70 80
3 100
30 69 70
Sample Output
Case 1: 2 758
Case 2: 3 777

題意:
假如你的ktv還有15秒就時間到了,但是當你正在唱一首歌的時候他們并不會把你趕走而是會等該歌曲播完才讓你離開,這時你可以點一首兩分鐘的歌,相當于你多唱了105秒,
假如你正在ktv唱歌,還剩下t秒時間,你決定接下來只唱你最愛的n首歌,在時間結束之前你會再點一首《勁歌金曲》(時長678秒),求出你一共唱了幾首歌和唱了多長時間,
輸入n(n<=50),t(t<=10^9)和每首歌的長度(保證不超過三分鐘),

解題思路
單純的思考所唱的時間最長,就是一個簡單的01背包問題,邊界有所不同是t-1(留一秒給勁歌金曲),

#include <stdio.h>
#include <stdlib.h>
#define Max(a,b) ((a)>(b)?(a):(b))
int main()
{
    int T,i,j;
    scanf("%d",&T);
    while(T--)
    {
        int n,t,time[50];
        int *dp;
        scanf("%d%d",&n,&t);
        dp=(int *)calloc(t,sizeof(int));   //初始化為0
        for(i=0;i<n;i++)
            scanf("%d",&time[i]);
        for(i=0;i<n;i++)
         for(j=t-1;j>=time[i];j--)
          dp[j]=Max(dp[j],dp[j-time[i]]+time[i]);
        printf("%d\n",dp[t-1]+678);
    }
    return 0;
}

那么我們再加入一個歌曲數的觀念,你會發現每次歌曲數增加都伴隨著dp(歌唱時間)的增加,那么我們只要將歌曲數的狀態方程放入回圈中同dp一起增加即可,

#include <stdio.h>
#include <stdlib.h>
#define Max(a,b) ((a)>(b)?(a):(b))
int main()
{
    int T,i,j;
    scanf("%d",&T);
    while(T--)
    {
        int n,t,time[50];
        int *dp,*count;
        scanf("%d%d",&n,&t);
        dp=(int *)calloc(t,sizeof(int));   //初始化為0
        count=(int *)calloc(t,sizeof(int));
        for(i=0;i<n;i++)
            scanf("%d",&time[i]);
        for(i=0;i<n;i++)
         for(j=t-1;j>=time[i];j--)
          {
              count[j]=Max(count[j],count[j-time[i]]+1);
              dp[j]=Max(dp[j],dp[j-time[i]]+time[i]);
          }
         printf("%d %d\n",count[t-1]+1,dp[t-1]+678);
    }
    return 0;
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/282739.html

標籤:其他

上一篇:漢諾塔(圖解演算+推導+Python實作)

下一篇:【c語言】 我使用c語言基礎做了一個老少皆宜的“國民小游戲“

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 2023年最新微信小程式抓包教程

    01 開門見山 隔一個月發一篇文章,不過分。 首先回顧一下《微信系結手機號資料庫被脫庫事件》,我也是第一時間得知了這個訊息,然后跟蹤了整件事情的經過。下面是這起事件的相關截圖以及近日流出的一萬條資料樣本: 個人認為這件事也沒什么,還不如關注一下之前45億快遞資料查詢渠道疑似在近日復活的訊息。 訊息是 ......

    uj5u.com 2023-04-20 08:48:24 more
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:47:46 more
  • vulnhub_Earth

    前言 靶機地址->>>vulnhub_Earth 攻擊機ip:192.168.20.121 靶機ip:192.168.20.122 參考文章 https://www.cnblogs.com/Jing-X/archive/2022/04/03/16097695.html https://www.cnb ......

    uj5u.com 2023-04-20 07:46:20 more
  • 從4k到42k,軟體測驗工程師的漲薪史,給我看哭了

    清明節一過,盲猜大家已經無心上班,在數著日子準備過五一,但一想到銀行卡里的余額……瞬間心情就不美麗了。最近,2023年高校畢業生就業調查顯示,本科畢業月平均起薪為5825元。調查一出,便有很多同學表示自己又被平均了。看著這一資料,不免讓人想到前不久中國青年報的一項調查:近六成大學生認為畢業10年內會 ......

    uj5u.com 2023-04-20 07:44:00 more
  • 最新版本 Stable Diffusion 開源 AI 繪畫工具之中文自動提詞篇

    🎈 標簽生成器 由于輸入正向提示詞 prompt 和反向提示詞 negative prompt 都是使用英文,所以對學習母語的我們非常不友好 使用網址:https://tinygeeker.github.io/p/ai-prompt-generator 這個網址是為了讓大家在使用 AI 繪畫的時候 ......

    uj5u.com 2023-04-20 07:43:36 more
  • 漫談前端自動化測驗演進之路及測驗工具分析

    隨著前端技術的不斷發展和應用程式的日益復雜,前端自動化測驗也在不斷演進。隨著 Web 應用程式變得越來越復雜,自動化測驗的需求也越來越高。如今,自動化測驗已經成為 Web 應用程式開發程序中不可或缺的一部分,它們可以幫助開發人員更快地發現和修復錯誤,提高應用程式的性能和可靠性。 ......

    uj5u.com 2023-04-20 07:43:16 more
  • CANN開發實踐:4個DVPP記憶體問題的典型案例解讀

    摘要:由于DVPP媒體資料處理功能對存放輸入、輸出資料的記憶體有更高的要求(例如,記憶體首地址128位元組對齊),因此需呼叫專用的記憶體申請介面,那么本期就分享幾個關于DVPP記憶體問題的典型案例,并給出原因分析及解決方法。 本文分享自華為云社區《FAQ_DVPP記憶體問題案例》,作者:昇騰CANN。 DVPP ......

    uj5u.com 2023-04-20 07:43:03 more
  • msf學習

    msf學習 以kali自帶的msf為例 一、msf核心模塊與功能 msf模塊都放在/usr/share/metasploit-framework/modules目錄下 1、auxiliary 輔助模塊,輔助滲透(埠掃描、登錄密碼爆破、漏洞驗證等) 2、encoders 編碼器模塊,主要包含各種編碼 ......

    uj5u.com 2023-04-20 07:42:59 more
  • Halcon軟體安裝與界面簡介

    1. 下載Halcon17版本到到本地 2. 雙擊安裝包后 3. 步驟如下 1.2 Halcon軟體安裝 界面分為四大塊 1. Halcon的五個助手 1) 影像采集助手:與相機連接,設定相機引數,采集影像 2) 標定助手:九點標定或是其它的標定,生成標定檔案及內參外參,可以將像素單位轉換為長度單位 ......

    uj5u.com 2023-04-20 07:42:17 more
  • 在MacOS下使用Unity3D開發游戲

    第一次發博客,先發一下我的游戲開發環境吧。 去年2月份買了一臺MacBookPro2021 M1pro(以下簡稱mbp),這一年來一直在用mbp開發游戲。我大致分享一下我的開發工具以及使用體驗。 1、Unity 官網鏈接: https://unity.cn/releases 我一般使用的Apple ......

    uj5u.com 2023-04-20 07:40:19 more