紫书P274

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

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

  1. #include <iostream>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <cstdio>
  5. using namespace std;
  6. const int MAX = 10000;
  7. int cnt[MAX],Time[MAX],song[MAX];
  8. int main()
  9. {
  10. int n,t;
  11. int tase,num = 0;
  12. scanf("%d", &tase);
  13. while( tase-- )
  14. {
  15. scanf("%d%d", &n, &t);
  16. for(int i = 1; i <= n; i++)
  17. scanf("%d", &song[i]);
  18. int v = t - 1;
  19. memset(cnt, 0, sizeof(cnt));
  20. memset(Time, 0, sizeof(Time));
  21. for(int i = 1; i <= n; i++)
  22. {
  23. for(int j = v; j >= song[i]; j--)
  24. {
  25. if(cnt[j] < cnt[j - song[i]] + 1) //选择第i个时的数量多
  26. {
  27. cnt[j] = cnt[j - song[i]] + 1;
  28. Time[j] = Time[j - song[i]] + song[i];
  29. }
  30. else if(cnt[j] == cnt[j - song[i]] + 1) //在数量相等的情况下,更新时间
  31. {
  32. if(Time[j] < Time[j - song[i]] + song[i])
  33. Time[j] = Time[j - song[i]] + song[i];
  34. }
  35. }
  36. }
  37. int res =678 + Time[v];
  38. printf("Case %d: %d %d\n",++num, cnt[v] + 1, res);
  39. }
  40. return 0;
  41. }

  

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. Python-执行系统命令

    执行系统命令 os.system os.spawn* os.popen popen2.* commands.* 后面三个已经废弃,以上执行shell命令的相关的模块和函数的功能均在subprocess ...

  2. Sublime Text 3 文本编辑器

    1.安装下载 下载地址:http://www.cr173.com/soft/121149.html http://www.xiazaiba.com/html/24343.html 官网 http:// ...

  3. uart串口的调试学习

    用FPGA设计了数据接收和发送模块,FIFO模块,此处FIFO调用的是Show-ahead模式,在下一篇博客中将会分析这个问题. 用串口调试工具发送数据,数据接收模块将接收到的串行数据转换为并行数据( ...

  4. 如何自定义kindeditor编辑器的工具栏items即去除不必要的工具栏或者保留部分工具栏

    kindeditor编辑器的工具栏主要是指编辑器输入框上方的那些可以操作的菜单,默认情况下编辑器是给予了所有的工具栏.针对不同的用户,不同的项目,不同的环境,可能就需要保留部分工具栏.那么我们应该如何 ...

  5. memcached缓存失效时的高并发访问问题解决

    memcached一般用于在访问一些性能相对低下的数据接口时(如数据库),为了保证这些数据接口的稳定性,加上memcached以减少访问次数,保证这些数据接口的健壮性.一般memcached的数据都是 ...

  6. 集成架构:对比 Web API 与面向服务的架构和企业应用程序集成(转)

    http://kb.cnblogs.com/page/521644/ 摘要:总体上讲,SOA 和 Web API 似乎解决的是同一个问题:以实时的.可重用的方式公开业务功能.本教程将分析这些举措有何不 ...

  7. Android Studio Gradle编译项目报错

    Gradle project sync failed Android Studio每次更新版本都会更新Gradle这个插件,但由于长城的问题每次更新都是失败,又是停止在Refreshing Gradl ...

  8. [MetaHook] Load large texture from model

    We need hook "GL_LoadTexture" engine function. GL_LOADTEXTURE_SIG from hw.dll(3266) engine ...

  9. Atom使用心得 - 21世纪的编辑器

    Atom使用心得 - 21世纪的编辑器 Atom下载 Atom简介:是专门为程序员推出的一个跨平台文本编辑器.具有简洁和直观的图形用户界面,并有很多有趣的特点:支持CSS,HTML,JavaScrip ...

  10. c#中的var优缺点和适用场景

    var是c# 3.0新加的特性,叫做隐式类型局部变量,大家都知道c#其实是一种强类型的语言,为什么会引入匿名类型呢? 我猜测是因为linq的原因吧,因为感觉var在linq中被大量使用.下面说下var ...