【Jason's_ACM_解题报告】Jin Ge Jin Qu hao

来源:互联网 发布:淘宝卖化妆品 编辑:程序博客网 时间:2024/05/07 06:18

Jin Ge Jin Qu hao

(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 ≤ 10^9 ), 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


首先一上来,t<=1000000000是用来唬人的,他说了“It is guaranteed that the sum of lengths of all songs (including Jin Ge Jin Qu) will be strictly larger than t.”所以,t至多只有50×3mins+678secs。所以此题最坏复杂度为O(N^3)也可以通过。

通过审题发现,这是一道变形的0-1背包问题。其中背包容量不是t而是t-1,因为我们要保证劲歌金曲可以留在最后唱,然后还要同时维护歌曲最大数目和歌曲最大数目时的最长歌唱时间,想到此,我们可以将歌唱时间当作体积,歌曲本身为重量,那么定义dp[i][j]为前i首歌一共唱了j秒其中有dp[i][j]首歌唱过。

如果顺序递推的话,边界处理为dp[0][i]=-1,即一首歌也不唱的时候,显然唱的时长若大于0则不可能出现这种情况,dp[0][0]=0,即一首歌也不长,所用时长为0。

我思考之后发现,逆序递推的本身及其边界处理是无法给他赋予现实意义的。另外他本质上就是是顺序递推,因为对于逆序递推来说n+1就是顺序递推的0、n是1...1是n、0是n+1。


附代码如下:

逆序递推:

#include<cstdio> #include<algorithm>using namespace std;#define MAXN (50+5)int n,t,len[MAXN];int dp[MAXN][MAXN*180+678];int main(){int T;scanf("%d",&T);for(int K=1;K<=T;K++){scanf("%d%d",&n,&t);for(int i=1;i<=n;i++)scanf("%d",&len[i]);for(int i=0;i<t;i++)dp[n+1][i]=-1;dp[n+1][0]=0;int ans=0;for(int i=n;i>=1;i--){for(int j=0;j<t;j++){dp[i][j]=dp[i+1][j];if(j>=len[i]&&dp[i+1][j-len[i]]>=0)dp[i][j]=max(dp[i][j],dp[i+1][j-len[i]]+1);ans=max(ans,dp[i][j]);}}for(int i=t-1;i>=0;i--){if(dp[1][i]==ans){printf("Case %d: %d %d\n",K,ans+1,i+678);break;}}}return 0;}

顺序递推:

#include<cstdio> #include<algorithm>using namespace std;#define MAXN (50+5)int n,t,len[MAXN];int dp[MAXN][MAXN*180+678];int main(){int T;scanf("%d",&T);for(int K=1;K<=T;K++){scanf("%d%d",&n,&t);for(int i=1;i<=n;i++)scanf("%d",&len[i]);for(int i=0;i<t;i++)dp[0][i]=-1;dp[0][0]=0;int ans=0;for(int i=1;i<=n;i++){for(int j=0;j<t;j++){dp[i][j]=dp[i-1][j];if(j>=len[i]&&dp[i-1][j-len[i]]>=0)dp[i][j]=max(dp[i][j],dp[i-1][j-len[i]]+1);ans=max(ans,dp[i][j]);}}for(int i=t-1;i>=0;i--){if(dp[n][i]==ans){printf("Case %d: %d %d\n",K,ans+1,i+678);break;}}}return 0;}

0 0
原创粉丝点击