Jin Ge Jin Qu [h]ao,Uva 12563

来源:互联网 发布:snmp trap 端口号 编辑:程序博客网 时间:2024/06/06 00:26
(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


题目介绍:紫书的例题,属于01背包问题的变体吧。处理起来稍微有些棘手地方在于首先拍歌曲多的情况,再考虑时间长的情况,所以处理时歌曲相同多的时候需要再一次判断时间是否会增加。如果会的话,保存时间的数组需要更新,否则就不需要更新了。我学习我happy。。。偷偷学

最后的Max时间不一定就是在deep[n][t-1]内,可能存在deep[n-1][i]中歌曲为最多的所有地方。所以遍历一下。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int const INF = 0x3f3f3f3f;int const MAXN = 10000+10;int mus[52],dp[52][MAXN],deep[52][MAXN];int main() {    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    int T,n,t,cnt = 1;    scanf("%d",&T);    while(T--) {        scanf("%d%d",&n,&t);        for(int i=1; i<=n; i++)            scanf("%d",&mus[i]);        memset(dp, 0,sizeof(dp));        memset(deep,0,sizeof(deep));        for(int i = 1; i<=n; i++)            for(int j = 0; j<=t; j++ ) {                dp[i][j]=(i==1)?0:dp[i-1][j];                deep[i][j]=deep[i-1][j];                if(j>=mus[i])                    if(dp[i][j]<dp[i-1][j-mus[i]]+1) {                        deep[i][j]=mus[i]+deep[i-1][j-mus[i]];                        dp[i][j]=dp[i-1][j-mus[i]]+1;                    } else if(dp[i][j]==dp[i-1][j-mus[i]]+1) {                        deep[i][j]=max(deep[i-1][j-mus[i]]+mus[i],deep[i-1][j]);                    }            }        if(cnt!=1)            printf("\n");        /*for(int i=0; i<=n; i++) {            for(int j=1; j<=t; j++) {                printf("%d  ",deep[i][j]);            }            cout<<endl;        }*/        int Max=-INF;        for(int i=t-1; i>=0; i--)        {            if(dp[n][i]==dp[n][t-1]&&Max<deep[n][i])            {                Max=deep[n][i];            }        }        printf("Case %d: %d %d",cnt++,dp[n][t-1]+1,Max+678);    }    cout<<endl;    return 0;}


原创粉丝点击