UVA Jin Ge Jin Qu hao 12563
(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 someother 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 songbefore 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!!!
Nowthat 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 alsomaximizes 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
题意:
KTV里面有n首歌曲你可以选择,每首歌曲的时长都给出了. 对于每首歌曲,你最多只能唱1遍. 现在给你一个时间限制t (t<=10^9) , 问你在最多t-1秒的时间内可以唱多少首歌曲num , 且最长唱歌时间是多少time (time必须<=t-1) ? 最终输出num+1 和 time+678 即可.
注意: 你需要优先让歌曲数目最大的情况下,再去选择总时长最长的.
思路:
其实本题本质上就是一个标准的01背包问题. 问你<=t-1时间内最多可以选择哪些歌曲使得 (数据1,数据2) 最优。 这里的数据1是歌曲数目,数据2是歌曲总时长,且数据1优先。
一般我们做的01背包问题都是问你<=t-1的时间内, 最多选择哪些歌曲使得歌曲数目最多或总时间最长。 但是本题需要同时考虑两个最优条件, 那么该怎么做呢?
我们令dp[i][j]==x 表示当决策完全前i个物品后(选或不选), 所选的总歌曲时长<=j时, 所得到的最优状态为x. (这里的x就不是平时我们所说的最长时间或最多歌曲数目了)
怎么理解最优状态为x这个事实呢? 假设有两种选择前i个歌曲的方法能使得决策完前i个物品且总时长<=j时的状态分别为x1 和x2.
那么如果x1状态的歌曲数目> x2状态的歌曲数目, 那么明显x1状态更优. 所以dp[i][j]应==x1.
如果x1状态的歌曲数目与x2的相等, 但是x2状态的时长 > x1状态时长, 那么此时x2状态更优. 所以dp[i][j]应==x2.
经过上面的分析,我们可以用一个(具有歌曲数和总时长双属性的)结构体来表示一个状态. 且可以得到下面状态转移公式:
dp[i][j] = 最优( dp[i-1][j] , 在dp[i-1][j-t[i]]的基础上选择第i首歌后得到的新状态tmp )
所有dp初始化为0即可. 最终我们所求为dp[n][max_time]
最后还有一个问题就是t<=10^9.我们不可能循环判断j到10^9. 其实一共50首歌曲, 每首歌曲最多180秒, 所以我们求出所有歌曲的时长和sum(sum<=50*180==9000).
如果t-1>=sum, 那么明显所有歌曲都能被选一遍.
如果t-1<sum,那么明显我们需要遍历到dp[i][t-1]为止.
程序实现用的滚动数组,所以dp只有[j]这一维.
错因:1.数组开小了。2.输入不能用cin,会RE。
#include<iostream> #include<cmath> #include<cstdio> #include<cstring> using namespace std; int T,n,t,sum,tot,num[60]; struct nond{ int num,time; }tmp,f[180*55+678]; nond max(nond a,nond b){ if(a.num==b.num) if(a.time<b.time) return b; else return a; if(a.num<b.num) return b; else return a; } int main(){ scanf("%d",&T); while(T--){ sum=0; memset(f,0,sizeof(f)); memset(num,0,sizeof(num)); scanf("%d%d",&n,&t); for(int i=1;i<=n;i++){ scanf("%d",&num[i]); sum+=num[i]; } t=min(t-1,sum); for(int i=1;i<=n;i++) for(int j=t;j>=num[i];j--){ tmp.num=f[j-num[i]].num+1; tmp.time=f[j-num[i]].time+num[i]; f[j]=max(f[j],tmp); } printf("Case %d: ",++tot); printf("%d %d\n",f[t].num+1,f[t].time+678); } return 0; }