Description
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
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
Sample Output
Case 1: 2 758
Case 2: 3 777
dp[j]表示前i首歌内恰用j时间最多能唱的歌曲数
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int N = 55, M = 180;
int c[N], d[N * M], t, n;
int main()
{
int cas, ans;
scanf("%d", &cas);
for(int k = 1; k <= cas; ++k)
{
memset(d, 0x8f, sizeof(d));
scanf("%d%d", &n, &t);
for(int i = 0; i < n; ++i) scanf("%d", &c[i]);
d[0] = 0;
for(int i = 0; i < n; ++i)
for(int j = t - 1; j >= c[i]; --j)
d[j] = max(d[j], d[j - c[i]] + 1); //唱或者不唱
for(int j = ans = t - 1; j >= 0; --j) //找出极值
if(d[j] > d[ans]) ans = j;
printf("Case %d: %d %d\n", k, d[ans] + 1, ans + 678);
}
return 0;
}