(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 extremelylong (11 minutes and 18 seconds) — I know that there are Jin Ge Jin Qu II and III, and some otherunofficial 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 extraseconds! ....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 thefollowing 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 wehave 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 positiveintegers 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, inseconds. 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 largerthan 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 Qufor 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 songinstead (30+70=100 seconds), the time is already up (since we only have 100 seconds in total), so wecan’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
题意:
一般来说,KTV不会在”时间到“的时候鲁莽地把你正在唱的歌切掉,而是会等它放完。例如,在还有15秒的时候再唱一首两分钟的歌,则实际上多唱了105秒。但是融合了37首歌曲的《劲歌金曲》长达11分18秒,如果唱这首,相当于多唱了663秒。
假定你正在唱KTV,还剩t秒时间。你决定接下来只唱你最爱的n首歌,(不含《劲歌金曲》)中的一些,在时间结束之前再唱一个《劲歌金曲》,使得唱的总曲目尽量多(包含《劲歌金曲》),在此前提下尽量晚的离开KTV。
输入n(n≤50),t(t≤10^9)和每首歌的长度(保证不超过3分钟),输出唱的总曲目及时间总长度。输入保证所有n+1首曲子的总长度严格大于t。
题解:
虽然说t≤10^9,但题目”保证所有n+1首曲子的总长度严格大于t“,即t<50*180+678=9678。这样一来就好办了。
dp[i][j]: 在第i首歌, 第j秒的时候, 唱的总歌曲数
dp[i][j]=dp[i-1][j]
dp[i][j]=dp[i-1][j-song[i]]+1
tim[i][j]: 在第i首歌, 第j秒的时候, 唱的总时间
tim[i][j]=tim[i-1][j]
tim[i][j]=tim[i-1][j-song[i]]+1
#include<iostream> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<algorithm> using namespace std; const int N=55; const int M=1e4; int n, t, song[N]; int dp[N][M], tim[N][M]; int T, cas, Jin=678; int main() { for( scanf( "%d", &T ); T; T-- ) { scanf( "%d%d", &n, &t ); for( int i=1; i<=n; i++ ) scanf( "%d", &song[i] ); memset( dp, 0, sizeof dp ); memset( tim, 0, sizeof tim ); for( int i=1; i<=n; i++ ) for( int j=0; j<=t-1; j++ ) { dp[i][j]=dp[i-1][j]; tim[i][j]=tim[i-1][j]; if( j>=song[i] ) { if( dp[i][j]<dp[i-1][ j-song[i] ]+1 ) { dp[i][j]=dp[i-1][ j-song[i] ]+1; tim[i][j]=tim[i-1][ j-song[i] ]+song[i]; } else if( dp[i][j]==dp[i-1][ j-song[i] ]+1 ) tim[i][j]=max( tim[i][j], tim[i-1][ j-song[i] ]+song[i] ); } } printf( "Case %d: %d %d\n", ++cas, dp[n][t-1]+1, tim[n][t-1]+Jin ); } return 0; }
[UVa12563]劲歌金曲
最新推荐文章于 2020-10-14 20:26:31 发布