(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]。

代码如下: 

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int NINF= -;
const int maxn=;
const int maxt=;
int t[maxn+];
int d[maxn+][maxt];
int song[maxn+][maxt];
int n,T; void solve(){
for(int j=T;j>=;j--){ //计算song[i][j]边界
if(j-t[n]>) song[n][j]=t[n];
else song[n][j]=;
}
for(int i=n;i>=;i--){
for(int j=T;j>;j--){
int v1,v2;
v1=d[i+][j]; //边界条件隐含在其中,当i==n时,d[i+1][]=0;
if(j-t[i]<=) v2=NINF;//如果j-t[i]<=0,让状态v2等于负无穷,一定小于v1
else v2=d[i+][j-t[i]]+;
d[i][j]=max(v1,v2);
//更新song
if(v2>v1){
song[i][j]=song[i+][j-t[i]]+t[i];
}
else if(v2==v1){
song[i][j]=max(song[i+][j],song[i+][j-t[i]]+t[i]);
}
else song[i][j]=song[i+][j];
}
}
}
int main(int argc, const char * argv[]) {
int kase;
scanf("%d",&kase);
for(int tt=;tt<=kase;tt++){
scanf("%d%d",&n,&T);
memset(t, , sizeof t);
for(int i=;i<=n;i++)
scanf("%d",&t[i]);
memset(d, , sizeof d);
memset(song, , sizeof song);
solve();
int num=d[][T]+;
int len=song[][T]+; printf("Case %d: %d %d\n",tt,num,len);
}
return ;
}

   

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

  1. 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 ...

  2. 12563 Jin Ge Jin Qu hao

    • Don’t sing a song more than once (including Jin Ge Jin Qu). • For each song of length t, either si ...

  3. UVA - 12563 Jin Ge Jin Qu hao (01背包)

    InputThe first line contains the number of test cases T (T ≤ 100). Each test case begins with two po ...

  4. uVa 12563 Jin Ge Jin Qu

    分析可知,虽然t<109,但是总曲目时间大于t,实际上t不会超过180*n+678.此问题涉及到两个目标信息,首先要求曲目数量最多,在此基础上要求所唱的时间尽量长.可以定义 状态dp[i][j] ...

  5. hdu2089(数位DP 递推形式)

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. hdu 2604 Queuing(dp递推)

    昨晚搞的第二道矩阵快速幂,一开始我还想直接套个矩阵上去(原谅哥模板题做多了),后来看清楚题意后觉得有点像之前做的数位dp的水题,于是就用数位dp的方法去分析,推了好一会总算推出它的递推关系式了(还是菜 ...

  7. Power oj2498/DP/递推

    power oj 2498 /递推 2498: 新年礼物 Time Limit: 1000 MS Memory Limit: 65536 KBTotal Submit: 12 Accepted: 3  ...

  8. BZOJ4321queue2——DP/递推

    题目描述 n 个沙茶,被编号 1~n.排完队之后,每个沙茶希望,自己的相邻的两 人只要无一个人的编号和自己的编号相差为 1(+1 或-1)就行:  现在想知道,存在多少方案满足沙茶们如此不苛刻的条件. ...

  9. Shell Necklace (dp递推改cdq分治 + fft)

    首先读出题意,然后发现这是一道DP,我们可以获得递推式为 然后就知道,不行啊,时间复杂度为O(n2),然后又可以根据递推式看出这里面可以拆解成多项式乘法,但是即使用了fft,我们还需要做n次多项式乘法 ...

随机推荐

  1. 【心有猛虎】react-pxq

    这是一个比较完整的简单的react移动端项目,说起来页面少,其实,构思若是精巧,也并不容易做 先放源码:https://github.com/bailicangdu/react-pxq 接下来我们简单 ...

  2. org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.trs.om.bean.User.retryCount

    六月 29, 2019 5:42:45 下午 org.apache.catalina.core.AprLifecycleListener init信息: The APR based Apache To ...

  3. cp和mv命令

    注意事项:mv与cp的结果不同,mv好像文件“搬家”,文件个数并未增加.而cp对文件进行复制,文件个数增加了. 一.cp命令 cp命令用来将一个或多个源文件或者目录复制到指定的目的文件或目录.它可以将 ...

  4. 【JZOJ4787】【NOIP2016提高A组模拟9.17】数格子

    题目描述 输入 输出 样例输入 1 10000 3 10000 5 10000 0 0 样例输出 1 11 95 数据范围 每个测试点数据组数不超过10组 解法 状态压缩动态规划. 设f[i][j]表 ...

  5. 探索云数据库最佳实践 阿里云开发者大会数据库专场邀你一起Code up!

    盛夏.魔都.科技 三者在一起有什么惊喜? 7月24日,阿里云峰会·上海——开发者大会将在上海世博中心盛大启程,与未来世界的开发者们分享数据库.云原生.开源大数据等领域的技术干货,共同探讨前沿科技趋势, ...

  6. HZOJ 毛一琛

    直接暴搜是$3^n$的,无法接受. 正解是$meet \ in \ the \ middle$,暴搜前n/2个数,每个数的状态有三种:都不选,选为A集合,选为B集合.那么我们可以维护两个集合的差. 设 ...

  7. IMEI校验思路及代码

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. js原生复习2.0

    // 1.闭包的作用// 实现共有变量,函数累加器的实现// 可以做缓存以及储存结构// 可以实现封装,实现属性私有化// 模块开发,防止全局污染// var name = 123;// var in ...

  9. Android横竖屏切换和灭屏亮屏时Activity的生命周期探究(1)

    研究这个问题的初衷在于项目中碰到了一个问题:横屏的时候灭屏再亮屏,亮屏的时候用户能够清晰的看到先启动竖屏(过程1)再切换到横屏的过程,因为灭屏的时候onSaveInstanceState()保存的时横 ...

  10. python 列表对象的增减