Problem J
Jin Ge Jin Qu [h]ao
(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)[1].
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 integersn, 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[2]. 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.
Sample Input
2 3 100 60 70 80 3 100 30 69 70
Output for the Sample Input
Case 1: 2 758 Case 2: 3 777
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!
Bonus
Be sure to test your program with the data provided in our gift package.
Notes
[1] 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.
[2] 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".
题意:你在KTV还剩t秒钟的时间 你需要在n首歌中选择尽量多的歌使得歌的数量最多的前提下,还剩下至少1秒的时间给劲歌金曲,输出最多可以唱的歌曲数目和总的唱歌时间。(劲歌金曲时间678s)。
解释:相当于求t-1的01背包,要选择尽量多的歌,可以以歌曲的时间为代价,每首歌曲的价值当作为1,从而容易求到t-1可以唱的最多首歌曲,但这样不能确定所唱歌曲的总时间,所以应求刚好满足t-1秒(若不存在刚好满足,即往前扫)的最多的歌曲数目,然后遍历dp[0~t-1]求出最大值即为t-1秒可以唱的最多歌曲数目,同时该最大值的下标即为所唱歌曲总时间,加上劲歌金曲时间就是答案。
# include <stdio.h> # include <string.h> # define MAXN 10000 # define INF 0x3f3f3f3f int main() { int dp[MAXN], maxsong, maxsongtime, a[51], t, T, n, result, time, i, j; scanf("%d",&T); for(t=1; t<=T; ++t) { memset(dp, -1, sizeof(dp));//要求刚好满足,除dp[0]外设为负。 maxsongtime = maxsong = dp[0] = 0; scanf("%d%d",&n,&time); for(i=0; i<n; ++i) scanf("%d",&a[i]); for(i=0; i<n; ++i) for(j=time-1; j>=a[i]; --j) { if(dp[j-a[i]]==-1) continue; else if(dp[j-a[i]]+1 > dp[j]) dp[j] = dp[j-a[i]] + 1; } for(i=time-1; i>=0; --i) { if(dp[i]>maxsong) { maxsong = dp[i]; maxsongtime = i; } } printf("Case %d: %d %d\n",t, maxsong+1, maxsongtime+678); } return 0; }