题意:给出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. ora-28002 the password will expire解决办法

    Oracle11g R2数据库提示ORA-28002: the password will expire within 5 days,是说密码过期,将Oracle密码设置成永不过期就可以了,不过并不推 ...

  2. java mail实现Email的发送,完整代码

    java mail实现Email的发送,完整代码 1.对应用程序配置邮件会话 首先, 导入jar <dependencies> <dependency> <groupId ...

  3. 剑指offer--面试题11

    题目:求数值的整数次方,不考虑大数值情况 即实现函数:double Power(double base, int exponent) 自己所写代码如下: #include "stdafx.h ...

  4. json封装与解析

    #include <iostream> #include <boost/property_tree/ptree.hpp> #include <boost/property ...

  5. PHP之SQL防注入代码集合(建站常用)

    SQL防注入代码一 <?php if (!function_exists (quote)) { function quote($var) { if (strlen($var)) { $var=! ...

  6. PHP 开发中的外围资源性能分析(一)

    暂且不讨论「PHP 是不是最好的编程语言」,本文我们将分别分析一下在 PHP 程序的后端外围资源和前端外围资源,它们对整个 PHP Web 应用体验的影响,这往往比语言本身大得多. 首先,后端外围资源 ...

  7. Linked List vs Array

    Both Arrays and Linked List can be used to store linear data of similar types, but they both have so ...

  8. DJANGO中,用QJUERY的AJAX的json返回中文乱码的解决办法

    和网上其它用JAVA或是PHP的实现不太一样, DJANGO中的解决办法如下: 后端样例: def render_to_json_response(context, **response_kwargs ...

  9. 各种实用的js,bootstrap插件

    1.nivoSlider  非常优秀的Banner轮播插件 2.BootstrapTable 表格插件使用技巧 = http://www.cnblogs.com/landeanfen/p/497683 ...

  10. Project Euler 88:Product-sum numbers 积和数

    Product-sum numbers A natural number, N, that can be written as the sum and product of a given set o ...