题意:给出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. ffmpeg 编码

    编码可以简单理解为将连续的图片帧转变成视频流的过程.以H264为例给出编码的代码: int InitEncoderCodec(int width, int height) { auto enc = a ...

  2. UVA 1175 Ladies' Choice 稳定婚姻问题

    题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...

  3. Eclipse插件安装总结

    Eclipse的插件安装分为在线安装和离线安装两类: 1.在线安装(新版本的推荐方式) 最常用和最好用的方式,直接使用Eclipse的Ecliplse Marketplace,搜索你需要的插件,然后点 ...

  4. [CF]codeforces round 369(div2)

    *明早起来再贴代码 A [题意] 给定n*5的方格 将横向的相邻两个变成+输出 [题解] ... B [题意] 一个n*n的正整数矩阵,有且仅有一个数为0 ,在这个位置填上一个数,使得每一列的和 每一 ...

  5. sampler2d

    Here is the syntax for a sampler in Direct3D 9. sampler Name = SamplerType{   Texture = <texture_ ...

  6. hdu 1754 线段树入门

    线段树点修改  区间最大值查询 #include <cstdio> #include <cstdlib> #include <cmath> #include < ...

  7. 《head first java 》读书笔记(二)

    Updated 2014/03/27 P402-P454 Updated 2014/04/03 P454- 世界三大首席管理器: border, flow, box borderLayout: 五个区 ...

  8. zoj 3716 Ribbon Gymnastics (思维数学题)

    题目 以四个顶点为圆心画圆,圆面积不能重合,求四个圆的直径和最大是多少. #define _CRT_SECURE_NO_WARNINGS #include<string.h> #inclu ...

  9. ZOJ 2724 Windows Message Queue (优先级队列,水题,自己动手写了个最小堆)

    #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...

  10. 青鸟 王云鹏老师写的SqlHelper 泛型方法,反射,支持实体类

    1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Text; 5: ...