12563 - Jin Ge Jin Qu hao——[DP递推]

(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

题目分析:

  每首歌最多选一次,由条件180n+678>T可知最大T=9678s,可以转化为0-1背包的问题:

  1.状态d[i][j]表示:在当前剩余时间为j的情况下,从i,i+1,…,n中能选出歌的最大数目。

  状态转移方程:d[i][j]=max{ d[i+1][j] , d[i+1][j-t[i]]+1 },( j-t[i]>0 );其中d[i+1][j]表示第i首歌未选时所选歌的最大数目,d[i+1][j-t[i]]+1表示第i首歌被选择后所选歌的最大数目。注意当 j-t[i]<=0 时 ,即剩余时间不大于0时,第i首歌不能选,此时d[i][j]=d[i+1][j];

  边界条件是:i>n,d[i][j]=0;

  2.由于题目要求在所点歌数目最大的情况下尽量保证唱歌的时间最长,那么同样可以转化成0-1背包问题,但是d[i][j]要先计算:

  状态song[i][j]表示:在当前剩余时间为j的情况下,从i,i+1,…,n中所选出歌累计的最长时间。

  状态转移跟随d[i][j]进行:令v1=d[i+1][j](即不选第i首歌),v2=d[i+1][j-t[i]]+1(选择第i首歌)

  如果:

    1) v2>v1,   说明第i首歌必须点,song[i][j]=song[i+1][j-t[i]]+t[i];

    2) v2==v1,  song[i][j]=max{song[i+1][j],song[i+1][j-t[i]]+t[i]};

    3) v2<v1,   说明第i首歌一定不能点,song[i][j]=song[i+1][j];

  逆序递推,答案是d[1][T]和song[1][T]。

代码如下: 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int  NINF= -100000000;
 6 const int maxn=50;
 7 const int maxt=10000;
 8 int t[maxn+5];
 9 int d[maxn+5][maxt];
10 int song[maxn+5][maxt];
11 int n,T;
12
13 void solve(){
14     for(int j=T;j>=0;j--){ //计算song[i][j]边界
15         if(j-t[n]>0) song[n][j]=t[n];
16         else song[n][j]=0;
17     }
18     for(int i=n;i>=1;i--){
19         for(int j=T;j>0;j--){
20             int v1,v2;
21             v1=d[i+1][j]; //边界条件隐含在其中,当i==n时,d[i+1][]=0;
22             if(j-t[i]<=0) v2=NINF;//如果j-t[i]<=0,让状态v2等于负无穷,一定小于v1
23             else v2=d[i+1][j-t[i]]+1;
24             d[i][j]=max(v1,v2);
25             //更新song
26             if(v2>v1){
27                 song[i][j]=song[i+1][j-t[i]]+t[i];
28             }
29             else if(v2==v1){
30                 song[i][j]=max(song[i+1][j],song[i+1][j-t[i]]+t[i]);
31             }
32             else song[i][j]=song[i+1][j];
33         }
34     }
35 }
36 int main(int argc, const char * argv[]) {
37     int kase;
38     scanf("%d",&kase);
39     for(int tt=1;tt<=kase;tt++){
40         scanf("%d%d",&n,&T);
41         memset(t, 0, sizeof t);
42         for(int i=1;i<=n;i++)
43             scanf("%d",&t[i]);
44         memset(d, 0, sizeof d);
45         memset(song, 0, sizeof song);
46         solve();
47         int num=d[1][T]+1;
48         int len=song[1][T]+678;
49
50         printf("Case %d: %d %d\n",tt,num,len);
51     }
52     return 0;
53 }

   

时间: 2024-10-26 10:07:46

12563 - Jin Ge Jin Qu hao——[DP递推]的相关文章

UVA 12563 Jin Ge Jin Qu hao 01背包变形

基本的01背包,更新的时候保持背包里每一个元素的num最大然后time尽量长 CSDN也支持makedown了试一下 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 popul

UVA Jin Ge Jin Qu hao 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 e

uva 12563 - Jin Ge Jin Qu hao(动态规划~劲!歌!金!曲!)

错的我真是无语...还是状态的把握不准确.. 起始状态转移方程是很重要,但是只推出了方程是不够的 对边界状态的处理,对特殊状态的处理,这些都很重要,错了任何一个小地方,都会导致WA.... 细节!更清晰的思路,更全面的考虑! #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,t; int v[55]; int d[55][10000]; int path[5

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

hdu2089(数位DP 递推形式)

不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 25802    Accepted Submission(s): 8967 Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以

CodeForces 372B 脑洞大开的DP递推

题目: 做了两个多小时,脑洞大开,给了一个01矩阵,求以a,b,为左上角,c,d为右下角的矩阵内有多少包含部分全为0的子矩阵 对于这道题目,一开始就想到了DP递推,感觉而已,虽然准,可是做不出啊,想好了递推式子可是细节部分没办法去处理.看了CF上的题解,顿时脑洞大开,这个做法真的是太厉害了,这方法代码简洁明了,同时也提醒到了我,在方程假设出来后,对于转移的细节处理, 其实一开始我想到过这个递推式子 dp[i][j][k][l] += dp[i][j][k - 1][l] + dp[i][j][k

D. Caesar&#39;s Legions 背包Dp 递推DP

http://codeforces.com/problemset/problem/118/D 设dp[i][j][k1][k2] 表示,放了i个1,放了j个2,而且1的连续个数是k1,2的连续个数是k2 如果这样写,用dfs写是很简单的.但是超时,我记忆化不到 如果用递推写,对于每一个状态,更新到下一个状态. 如果放的是1,那么新的状态是dp[i + 1][j][k1 + 1][0]也就是,用多了一个1,而且连续的个数也增加了.同时,2的连续个数就打破了,变成了0 这种枚举旧状态,更新下一个状态

HDU Tickets(简单的dp递推)

Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 972    Accepted Submission(s): 495 Problem Description Jesus, what a great movie! Thousands of people are rushing to the cinema. However,

玲珑杯 #10 A dp递推

A. Black and White 题意:n个格子排在一行,每个格子里都有一枚白棋或一枚黑棋.限制:不能有连续a枚黑棋或连续b枚白棋,问有多少种方案. tags:一开始还以为是组合数学,没想到又是dp.这mmp的还是签到题 考虑长度为 i 的合法序列与长度为 i−1 的合法序列有什么关系.定dp[i][black] 为有 i 枚棋子且最后一枚是黑色的合法方案数量,g[i]为有 i 枚棋子的合法方案数量.现假设前面 i-1 枚棋都是合法的,在 i 位置加一枚黑棋,唯一不合法的情况就是最后的a枚棋