紫书P274

题意:输入N首歌曲和最后剩余的时间t,问在保证能唱的歌曲数目最多的情况下,时间最长;最后必唱《劲歌金曲》

所以就在最后一秒唱劲歌金曲就ok了,背包容量是t-1,来装前面的歌曲,设两个Time求时间,cnt是数量

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAX = 10000;
int cnt[MAX],Time[MAX],song[MAX];
int main()
{
int n,t;
int tase,num = 0;
scanf("%d", &tase);
while( tase-- )
{
scanf("%d%d", &n, &t);
for(int i = 1; i <= n; i++)
scanf("%d", &song[i]);
int v = t - 1;
memset(cnt, 0, sizeof(cnt));
memset(Time, 0, sizeof(Time));
for(int i = 1; i <= n; i++)
{
for(int j = v; j >= song[i]; j--)
{
if(cnt[j] < cnt[j - song[i]] + 1) //选择第i个时的数量多
{
cnt[j] = cnt[j - song[i]] + 1;
Time[j] = Time[j - song[i]] + song[i];
}
else if(cnt[j] == cnt[j - song[i]] + 1) //在数量相等的情况下,更新时间
{
if(Time[j] < Time[j - song[i]] + song[i])
Time[j] = Time[j - song[i]] + song[i];
}
}
}
int res =678 + Time[v];
printf("Case %d: %d %d\n",++num, cnt[v] + 1, res);
}
return 0;
}

  

UVA12563Jin 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. UVA12563-Jin Ge Jin Qu hao(动态规划基础)

    Problem UVA12563-Jin Ge Jin Qu hao Accept: 642  Submit: 7638Time Limit: 3000 mSec Problem Descriptio ...

  3. UVa12563- Jin Ge Jin Qu hao

    思路一定要清晰! /* * Author: Bingo * Created Time: 2014/12/25 3:45:35 * File Name: uva12563.cpp */ #include ...

  4. UVA 12563 "Jin Ge Jin Qu hao" (背包)

    传送门 debug了好一会,突然发现,输出错了,emmm......... 明天再写debug历程: (PS:ipad debug是真的繁琐) 题意: 题解: 尽管题干中给的 t 的范围很大,但是 t ...

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

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

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

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

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

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

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

随机推荐

  1. 报错"the geometry has no Z values"处理

    );  //将Z值设置为0 //IPoint point = (IPoint)pGeo; //point.Z = 0; } else            {                IZAwa ...

  2. typicalapp.js

    /** * 1.找出数字数组中最大的元素(使用Math.max函数) 2.转化一个数字数组为function数组(每个function都弹出相应的数字) 3.给object数组进行排序(排序条件是每个 ...

  3. zepto源码注解

    /* Zepto v1.0-1-ga3cab6c - polyfill zepto detect event ajax form fx - zeptojs.com/license */ ;(funct ...

  4. python算法:rangeBitwiseAnd(连续整数的与)

    def rangeBitwiseAnd(self, m, n): i = 0 while m != n: m >>= 1 n >>= 1 i += 1 return n < ...

  5. JS实现点击跳转登陆邮箱

    前言 注册的过程中往往需要填写邮箱,并登陆邮箱进行验证.利用JS可以实现针对不同的邮箱进行点击登录验证,以下为实现方案,很简单 代码 邮箱域名数据   1 2 3 4 5 6 7 8 9 10 11 ...

  6. python 转 exe -- py2exe库实录

    本文基于windows 7 + python 3.4 把python程序打包成exe,比较好用的库是py2exe 其操作步骤是: --> 编写python程序 --> 再额外编写一个导入了 ...

  7. python数字图像处理(12):基本图形的绘制

    图形包括线条.圆形.椭圆形.多边形等. 在skimage包中,绘制图形用的是draw模块,不要和绘制图像搞混了. 1.画线条 函数调用格式为: skimage.draw.line(r1,c1,r2,c ...

  8. chrome扩展

    chrome拓展开发实战:页面脚本的拦截注入 时间 2015-07-24 11:15:00  博客园精华区 原文  http://www.cnblogs.com/horve/p/4672890.htm ...

  9. LeetCode-Count Univalue Subtrees

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

  10. CodeForces 166E -Tetrahedron解题报告

    这是本人写的第一次博客,学了半年的基础C语言,初学算法,若有错误还请指正. 题目链接:http://codeforces.com/contest/166/problem/E E. Tetrahedro ...