题意:给出t秒时间,n首歌分别的时间a[i],还给出一首长度为678的必须唱的劲歌金曲,问最多能够唱多少首歌(只要最后时间还剩余一秒,都可以将劲歌金曲唱完)

用dp[i]代表花费i时间时唱的歌的最大数量 背包容量即为给出的总时间t-1(留一秒钟唱劲歌金曲)

,每首歌的代价为a[i], 然后状态转移方程为

dp[j]=max(dp[j],dp[j-a[i]]+1);

自己写的时候,一直一直wa 后来看了lrj的代码,发现是初始化不对, 改掉初始化就对了= =

可是为什么要这样初始化呢= =

后来才明白,给初始状态赋特殊的值,是因为这个状态是不合法的,不能从这个状态开始转移 比如说如果初始值都赋为0的话,dp[99]=0,那么就是一首歌都还没有唱,就已经花费了99秒钟,

所以应该初始化为-1(-2,-3,-4-5,-6,-7-------等其他取不到的特殊值)

 #include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
#define mod=1e9+7;
using namespace std; typedef long long LL;
const int maxn=;
int a[maxn],dp[maxn]; int main(){
int n,t,i,j,ncase,kase=;
cin>>ncase;
while(ncase--){ cin>>n>>t;
for(i=;i<=n;i++) cin>>a[i]; int ans=; for(int i = ; i < t; i++) dp[i] = -;
dp[] = ; for(i=;i<=n;i++){
for(j=t-;j>=;j--){
if(j>=a[i]) dp[j]=max(dp[j],dp[j-a[i]]+); ans=max(ans,dp[j]);
// printf("ans=%d\n",ans);
// printf("dp[%d]=%d\n",j,dp[j]);
}
} for(i=t-;i>=;i--){
if(dp[i]==ans){
printf("Case %d: %d %d\n", ++kase, ans + , i + );
break;
}
}
}
return ;
}

UVa 12563 Jin Ge Jin Qu hao【01背包】的更多相关文章

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

  2. UVA12563Jin Ge Jin Qu hao(01背包)

    紫书P274 题意:输入N首歌曲和最后剩余的时间t,问在保证能唱的歌曲数目最多的情况下,时间最长:最后必唱<劲歌金曲> 所以就在最后一秒唱劲歌金曲就ok了,背包容量是t-1,来装前面的歌曲 ...

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

  4. uVa 12563 Jin Ge Jin Qu

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

  5. 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, se ...

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

  7. UVa 12563 (01背包) Jin Ge Jin Qu hao

    如此水的01背包,居然让我WA了七次. 开始理解错题意了,弄反了主次关系.总曲目最多是大前提,其次才是歌曲总时间最长. 题意: 在KTV房间里还剩t秒的时间,可以从n首喜爱的歌里面选出若干首(每首歌只 ...

  8. Jin Ge Jin Qu hao UVA - 12563 01背包

    题目:题目链接 思路:由于t最大值其实只有180 * 50 + 678,可以直接当成01背包来做,需要考虑的量有两个,时间和歌曲数,其中歌曲优先级大于时间,于是我们将歌曲数作为背包收益,用时间作为背包 ...

  9. 一道令人抓狂的零一背包变式 -- UVA 12563 Jin Ge Jin Qu hao

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

  10. 【紫书】(UVa12563)Jin Ge Jin Qu hao

    继续战dp.不提. 题意分析 这题说白了就是一条01背包问题,因为对于给定的秒数你只要-1s(emmmmm)然后就能当01背包做了——那1s送给劲歌金曲(?).比较好玩的是这里面dp状态的保存——因为 ...

随机推荐

  1. 不借助jquery封装好的ajax,你能用js手写ajax框架吗

    不借助jquery封装好的ajax,你能手写ajax框架吗?最基础的ajax框架,一起来围观吧. .创建XMLHttpRequest对象 var xhr = new XMLHttpRequest(); ...

  2. centos nginx,php添加到Service

    SHELL脚本: nginx vim /etc/init.d/nginx #!/bin/sh # # nginx - this script starts and stops the nginx da ...

  3. c++ memset 函数 及 坑

    #include <string.h> #include <stdio.h> typedef struct ss{ int num; ][]; }tent; tent a; i ...

  4. android控件---spinner

    spinner下拉列表框的列表项有两种配置方式: 1.通过资源文件配置,通过在values种的xml,比如strings.xml中使用<string-array>元素添加制定列表项内容,然 ...

  5. httpsClient

    Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客户端发送Http请求变得容易,而且 ...

  6. linux服务器重启服务命令说明文档

    (前提是电脑上面已经安装好了ssh软件~!)输入ip,用户名,端口(默认22) 输入密码,登陆成功之后, 转入到/usr/local/tomcat/bin 目录,输入命令行: [root@yangch ...

  7. 精华阅读第 10 期 |解开阿尔法狗(AlphaGo)人工智能的画皮

    谷歌用一个变了身的古老「穷举算法」,披上「神经网络」的画皮,假装「跨时代」的黑科技,忽悠广大「膜拜者」,「狮仙」我实在看不下去了,来揭一揭这只幺蛾子小狗的画皮. 本期是移动开发精英俱乐部的第10期推荐 ...

  8. 暑假集训单切赛第一场 CF 266E More Queries to Array(线段树+二项式展开式)

    比赛时,第二题就是做的这个,当时果断没仔细考虑,直接用线段树暴力求.结果易想而知,超时了. 比赛后搜了搜题解,恍然大悟. 思路:显然用线段树,但是由于每次查询都会有变,所以不可能存储题目中的式子.   ...

  9. POJ 1922

    #include<iostream>cheng da cai zi 11.21 //#include<stdio.h> #include<math.h> using ...

  10. POJ1442Black Box

    http://poj.org/problem?id=1442 题意 : 题目中对给出的数字有两种操作ADD(I)操作,将ADD括号里的数字 I 加到数列里边去,然后是自动排好序的,每一个数列前边都会有 ...