Jin Ge Jin Qu hao UVA - 12563 (劲歌金曲)01背包,求装入的东西最多(相同多时价值大)
題目:白書p274
題意:?
KTV里面有n首歌曲你可以選擇,每首歌曲的時長都給出了. 對于每首歌曲,你最多只能唱1遍. 現在給你一個時間限制t (t<=10^9) , 問你在最多t-1秒的時間內可以唱多少首歌曲num , 且最長唱歌時間是多少time (time必須<=t-1) ? 最終輸出num+1 和 time+678 即可.?
注意: 你需要優先讓歌曲數目最大的情況下,再去選擇總時長最長的.?
解析:?
每種歌曲只能用一次,所以是比較水的01背包題?
最后枚舉找到最大曲目數量并且時間盡量靠后即可
題目中t的范圍是1e9但實際的t不會超過10000,這就可以轉化為01背包問題
(If you smiled when you see the title, this problem is for you ^_^)
For those who don’t know KTV, see: http://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!
Sample Input
2
3 100
60 70 80
3 100
30 69 70
Sample Output
Case 1: 2 758
Case 2: 3 777
#include<stdio.h> #include<string.h> #include<map> #include<algorithm> using namespace std; int t,m,n; int dp[110]; int w[10000010]; int main() {int k=1;scanf("%d",&t);while(t--){scanf("%d%d",&m,&n);for(int i=0; i<m; i++)scanf("%d",&dp[i]);memset(w,-1,sizeof(w));w[0]=0;/**care*/for(int i=0; i<m; i++)for(int j=n-1; j>=dp[i]; j--)w[j]=max(w[j],w[j-dp[i]]+1);int ans=0,sum;for(int i=0;i<=n;i++){if(ans<=w[i]){ans=w[i];sum=i;}}printf("Case %d: %d %d\n",k++,ans+1,sum+678);}return 0; }?
總結
以上是生活随笔為你收集整理的Jin Ge Jin Qu hao UVA - 12563 (劲歌金曲)01背包,求装入的东西最多(相同多时价值大)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 奇富科技2023年Q3净利润11.38亿
- 下一篇: 索尼 PSN 商店“黑五”折扣上线:PS