UVA - 12563
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 shouldselect 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 lengthsof 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 secondleft, 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!
The question is from here
My Solution:
刘老师给了我们那么多的数据是为了告诉我们: 歌曲的总时间不会超过 n*180-1+678 及 50*180-1+678 = 9677
并不像题中的t <= 10^9那么吓人,最多 9677了,所以可以dp[ n ] [ t ];求最大歌曲数,且在此前提下时间尽可能长
//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.
题目中的这句话当时没有理解对,多花了好多时间,因为如果 d , dw 分开 dp 则可能时间最多的时候歌曲数不是最多,这样就WA了
而事实上是leave KTV as late as possible when there are ties. 要使用if语句 搞一起
算法 :0-1背包问题的方法 用 规划方向,边读入边计算
#include <iostream> #include <cstdio> #include <cstring> //#define LOCAL using namespace std; int d[52][40000],dw[52][40000]; int main() { #ifdef LOCAL freopen("a.txt","r",stdin); #endif // LOCAL int T,n,t,w,ans, kase = 1; scanf("%d", &T); while(T--){ ans = 0; scanf("%d%d", &n, &t); for(int i = 1; i <= n; i++){ scanf("%d", &w); for(int j = t; j > 0; j--){ d[i][j] = (i == 1 ? 0 : d[i-1][j]); /* 像这样分开 dp 在这里是错的 if(j > w) d[i][j] = max(d[i][j], d[i-1][j-w]+1); dw[i][j] = (i == 1 ? 0 : dw[i-1][j]); if(j > w ) dw[i][j] = max(dw[i][j], dw[i-1][j-w]+w); */ dw[i][j] = (i == 1 ? 0 : dw[i-1][j]); if(j > w){ int& a = d[i][j]; int& b = d[i-1][j-w]; if(a < b+1) {a = b+1; dw[i][j]=dw[i-1][j-w]+w;} else if(a == b+1) dw[i][j] = max(dw[i][j],dw[i-1][j-w]+w); } } } //求 max{d[n][i]}即为最大歌曲数 for(int i = 1; i <= t; i++){ if(d[n][i] > ans) ans = d[n][i];// time = i;} } //cout<<dw[n][t]<<endl; printf("Case %d: ",kase++); printf("%d %d\n",ans+1,dw[n][t]+678); } return 0; }