UVA12563 Jin Ge Jin Qu hao
Posted 嘒彼小星
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA12563 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 ≤ 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
https://odzkskevi.qnssl.com/a490fe31d294d55ff37b786cd582fa76?v=1508184276
【题解】
经典01背包加一个状态:len[i][j]表示前i手歌曲,唱了<=j时间,最大延长长度
len[i][j] = len[i - 1][j], len[i - 1][j - v[i]] + v[i]
注意最终答案是T - 1而非T!
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstdlib> 5 #include <cstring> 6 #define max(a, b) ((a) > (b) ? (a) : (b)) 7 #define min(a, b) ((a) < (b) ? (a) : (b)) 8 inline void swap(int &a, int &b) 9 { 10 int tmp = a;a = b;b = tmp; 11 } 12 inline void read(int &x) 13 { 14 x = 0;char ch = getchar(), c = ch; 15 while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar(); 16 while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar(); 17 if(c == ‘-‘)x = -x; 18 } 19 20 const int MAXN = 100 + 5; 21 const int MAXV = 180 * 50 + 678 + 10; 22 const int INF = 0x3f3f3f3f; 23 24 int dp[MAXV*2], len[MAXV*2], n, v[MAXN], t, T; 25 int ans1, ans2; 26 27 int main() 28 { 29 read(t); 30 for(register int tt = 1;tt <= t;++ tt) 31 { 32 read(n);read(T); 33 memset(len, 0, sizeof(len)); 34 memset(dp, 0, sizeof(dp)); 35 for(register int i = 1;i <= n;++ i) read(v[i]); 36 for(register int i = 1;i <= n;++ i) 37 for(register int j = T;j >= v[i];-- j) 38 { 39 if(dp[j] < dp[j - v[i]] + 1) 40 { 41 dp[j] = dp[j - v[i]] + 1; 42 len[j] = len[j - v[i]] + v[i]; 43 } 44 else if(dp[j] == dp[j - v[i]] + 1) 45 { 46 len[j] = max(len[j], len[j - v[i]] + v[i]); 47 } 48 } 49 printf("Case %d: %d %d\n", tt, dp[T - 1] + 1, len[T - 1] + 678); 50 } 51 return 0; 52 }
以上是关于UVA12563 Jin Ge Jin Qu hao的主要内容,如果未能解决你的问题,请参考以下文章
UVA12563Jin Ge Jin Qu hao(01背包)
[Uva12563] Jin Ge Jin Qu hao (完全背包,dp)
UVA 12563 Jin Ge Jin Qu hao DP