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 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[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
题意:
1,在时间t内唱的歌数量越多越好;
2,还要求唱歌总时间越长越好;
至少留出一秒钟来唱jin ge jin qu;因为必须选择唱jin ge jin qu 才能最优;
所以用t-1时间来选择唱给出的n首歌;
尽量唱的数量多,在数量相同时尽量时间长;
思路:把时间当做花费,数量当做价值,进行01背包,因为由于递推的原因会导致无法确认数量最大时,时间是多少;所以要初始化dp为一个特殊值来判断;即完全背包;
这样就可以保证数量最大时刻的时间处为耗费的总时间;
链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34887
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<string>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
int dp[10000];
const int inf = 0x3f3f3f3f;
int next[55];
int a[55], t, n;
int main()
{
int T, cas=1;
cin>>T;
while
(T--)
{
scanf(
"%d%d"
,&n,&t);
for
(int i = 1; i <= n; i++){
scanf(
"%d"
,&a[i]);
}
for
(int i = 1; i < 10000; i++) dp[i] = -inf;
dp[0] = 0;
for
(int i = 1; i <= n; i++){
for
(int j = t-1; j >= a[i]; j--){
dp[j] = max(dp[j],dp[j-a[i]]+1);
}
}
int time ,cnt, mas = -inf, pos;
for
(int i = t-1; i >= 0; i--){
if
(dp[i]>mas){
mas = dp[i]; pos = i;
}
}
printf(
"Case %d: %d %d\n"
,cas++,mas+1,pos+678);//最后一秒用来唱jin ge jin qu;
}
return
0;
}