dp-背包

开始用普通dp写了一发发现没法确定最大时间。。。

后来看到大牛机智的写法,嗯。。。dp表示当前状态能否成立;然后从条件最好的状态开始遍历,直到这个状态成立然后退出遍历。

具体的看代码吧。。。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <algorithm>
 5 using namespace std;
 6
 7 int dp[100][10000];
 8 int d[100];
 9
10 int main (){
11     int T,kase=0;
12     cin>>T;
13     while (T--){
14         int n,t;
15         cin>>n>>t;
16         for (int i=0;i<n;i++){
17             cin>>d[i];
18         }
19         sort (d,d+n);
20         memset (dp,0,sizeof dp);
21         dp[0][0]=1;
22         for (int k=0;k<n;k++){
23             for (int j=t;j>=d[k];j--){
24                 for (int i=k+1;i>0;i--){
25                     if (dp[i-1][j-d[k]])
26                         dp[i][j]=1;
27                 }
28             }
29         }
30         int ans=0;
31         int time;
32         int flag=0;
33         for (int i=n;i>=0;i--){
34             for (int j=t-1;j>=0;j--){//cout<<j<<" ";
35                 if (dp[i][j]){
36                     ans=i;
37                     time=j;
38                     flag=1;
39                     break ;
40                 }
41             }
42             if (flag)
43                 break ;
44         }
45         cout<<"Case "<<++kase<<": "<<ans+1<<" "<<time+678<<endl;
46     }
47     return 0;
48 }

转载于:https://www.cnblogs.com/gfc-g/p/3878324.html

UVA 12563 Jin Ge Jin Qu hao相关推荐

  1. UVA 12563 Jin Ge Jin Qu hao 01背包变形

    基本的01背包,更新的时候保持背包里每一个元素的num最大然后time尽量长 CSDN也支持makedown了试一下 12563 Jin Ge Jin Qu hao (If you smiled wh ...

  2. UVA 12563 劲歌金曲 Jin Ge Jin Qu hao

    劲歌金曲 Jin Ge Jin Qu hao 题面翻译 (如果当你看到这个标题的时候笑了,那么这个问题是为你准备的ヽ( ̄▽ ̄)ノ) 如果问一个麦霸:"你在KTV里必唱的曲目有哪些?" ...

  3. Jin Ge Jin Qu hao UVA - 12563 (劲歌金曲)01背包,求装入的东西最多(相同多时价值大)

    题目:白书p274 题意:  KTV里面有n首歌曲你可以选择,每首歌曲的时长都给出了. 对于每首歌曲,你最多只能唱1遍. 现在给你一个时间限制t (t<=10^9) , 问你在最多t-1秒的时间 ...

  4. Jin Ge Jin Qu hao - UVa 12563 dp背包

    Problem J Jin Ge Jin Qu [h]ao (If you smiled when you see the title, this problem is for you ^_^) Fo ...

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

  6. UVA12563: Jin Ge Jin Qu hao(类01背包)

    Problem J Jin Ge Jin Qu [h]ao (If you smiled when you see the title, this problem is for you ^_^) Fo ...

  7. uva 12563——Jin Ge Jin Qu hao

    题意:给出n首歌及每首歌的播放时间,然后在t秒内唱这些歌,最后的剩余时间要大于0: 思路:01背包问题,对于没次选择,只有取或不取两种状态,只需在这两种状态中找到最优的策略即可. code: #inc ...

  8. UVA 12563 Jin Ge Jin Qu hao(多阶段决策问题,DP)

    题意:给出n首爱唱的歌,剩余t时间.由于ktv最后不会强制暂停你的歌曲,所以最后你可以点一首劲歌金曲,也就是最后可以加上678秒多唱一些时间.问在保证能唱的歌曲尽量多的情况下,唱歌的时间尽量长.输出最 ...

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

    题意  你在KTV还剩t秒钟的时间  你需要在n首歌中选择尽量多的歌使得歌的数量最多的前提下剩下的时间最小 至少要留一秒给劲歌金曲  所以是一个容量为t-1的01背包   d[i][j]表示恰用j秒时 ...

最新文章

  1. cocos2d-x在win7下的android交叉编译环境
  2. js ==与===区别(两个等号与三个等号)
  3. [How TO]-ubuntu20.10上安装Pulse Secure客户端
  4. 华师大数据科学考研_华东师范大学数据科学与工程需要复习哪些内容?
  5. 项目评测(共27组)
  6. CSS animation 与 transition 有何区别?
  7. 软件项目管理案例教程第四版答案
  8. c语言构造数学模型步骤,数学建模的6个基本步骤
  9. 【Verilog】一文了解verilog基础语法
  10. 学好英语网html首页制作,首页英语
  11. 使用python制作一个刷屏器,桌面exe文件
  12. 游戏对战平台研究终结篇【转】
  13. Iptables入门
  14. 阿里网盘阿里云盘----手机端PC端
  15. 爬虫有道词典进行自动翻译
  16. win7降低屏幕亮度_Win7电脑屏幕亮度怎么调?Win7调节屏幕亮度的步骤
  17. python中callable什么意思_Python中的callable是基于什么样的机制实现的
  18. PSINS工具箱15状态组合导航仿真程序(test_SINS_GPS_153)浅析-卡尔曼滤波设置+导航解算
  19. 给你的PPT加上美观实用的导航自我介绍ppt模板
  20. 华为部门宣讲会后感及求职的一点体会

热门文章

  1. 【转】30种MySQL索引优化的方法
  2. Java Web开发技术教程入门-JavaBean组件与Servlet
  3. css笔记——css 实现自定义按钮
  4. ASP.NET MVC5+EF6+EasyUI 后台管理系统(51)-系统升级
  5. uoni扫地机器人好用吗_扫地机器人好用吗?了解性能看这篇
  6. nginx 配置 http/2(h2) 和 http 在同一端口的问题
  7. elasticsearch 6.x (五) 单一文档 API 介绍和使用 update和delete API
  8. 数组追加数组,小程序数组里面追加数组如何操作?
  9. error: style attribute '@android:attr/windowEnterAnimation' not found
  10. iview代码片段 去除水平menu底部横线