UVA 624 CD(DP + 01背包)
| CD |
You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is on CDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. How to choose tracks from CD to get most out of tape space and have as short unused space as possible.
Assumptions:
- number of tracks on the CD. does not exceed 20
- no track is longer than N minutes
- tracks do not repeat
- length of each track is expressed as an integer number
- N is also integer
Program should find the set of tracks which fills the tape best and print it in the same sequence as the tracks are stored on the CD
Input
Any number of lines. Each one contains value
N, (after space) number of tracks and durations of the tracks. For example from first line in sample data:
N=5, number of tracks=3, first track lasts for 1 minute, second one 3 minutes, next one 4 minutes
Output
Set of tracks (and durations) which are the correct solutions and string ``
sum:" and sum of duration times.
Sample Input
5 3 1 3 4
10 4 9 8 4 2
20 4 10 5 7 4
90 8 10 23 1 2 3 4 5 7
45 8 4 10 44 43 12 9 8 2
Sample Output
1 4 sum:5
8 2 sum:10
10 5 4 sum:19
10 23 1 2 3 4 5 7 sum:55
4 10 12 9 8 2 sum:45
题意:给定一个时间上限和n个cd盘,每个cd有一个播放时间,要求出不超过时间上限的情况下最多的可以播放的时间以及使用的cd。
思路:01背包。状态转移方程为dp[j - cd[i] == 1 ? dp[j] = 1 : dp[j] = 0。就是多个要保存下路径。挺裸的一题
代码;
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int sum, n, cd[25], i, j, k, dp[1111], out[1111][25], outn[1111]; int main() {
while (~scanf("%d", &sum)) {
scanf("%d", &n);
memset(dp, 0, sizeof(dp));
memset(out, 0, sizeof(out));
memset(outn, 0, sizeof(outn));
dp[0] = 1;
for (i = 0; i < n; i ++)
scanf("%d", &cd[i]);
for (i = 0; i < n; i ++)
for (j = sum; j >= cd[i]; j --) {
if (dp[j - cd[i]] && !dp[j]) {
dp[j] = 1;
for (k = 0; k < outn[j - cd[i]]; k ++)
out[j][outn[j] ++] = out[j - cd[i]][k];
out[j][outn[j] ++] = cd[i];
}
}
for (i = sum; i >= 0; i --) {
if (dp[i]) {
sort(out[i], out[i] + outn[i]);
for (j = 0; j < outn[i]; j ++) {
printf("%d ", out[i][j]);
}
printf("sum:%d\n", i);
break;
}
}
}
return 0;
}
UVA 624 CD(DP + 01背包)的更多相关文章
- UVA 624 CD(01背包+输出方案)
01背包,由于要输出方案,所以还要在dp的同时,保存一下路径. #include <iostream> #include <stdio.h> #include <stri ...
- uva 624 CD (01背包)
CD You have a long drive by car ahead. You have a tape recorder, but unfortunately your best musi ...
- UVA 624 CD[【01背包】(输出路径)
<题目链接> 题目大意: 你要录制时间为N的带子,给你一张CD的不同时长的轨道,求总和不大于N的录制顺序 解题分析: 01背包问题,需要注意的是如何将路径输出. 由于dp时是会不断的将前面 ...
- UVA 624 CD【01背包+路径记录】
You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is o ...
- UVA 624 CD(01背包,要记录路径)
题意: 有n张CD(n<=20),每张能播放的时长不同.给定一个时长限制t,挑出部分的CD使得总播放时间最长.顺便输出路径! 思路: 重点在输出路径,否则这题很普通.那就要用二维数组记录每个CD ...
- UVA.10130 SuperSale (DP 01背包)
UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...
- POJ-1015 Jury Compromise(dp|01背包)
题目: In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting ...
- USACO Money Systems Dp 01背包
一道经典的Dp..01背包 定义dp[i] 为需要构造的数字为i 的所有方法数 一开始的时候是这么想的 for(i = 1; i <= N; ++i){ for(j = 1; j <= V ...
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
随机推荐
- ZeptoLab Code Rush 2015 A. King of Thieves 暴力
A. King of Thieves Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/526/pr ...
- Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索
Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- YS报警权限验证安全设计
1.总体流程图: 备注: 1. 使用加时间戳的方式优点是可以不用对报警请求进行加密,也可以防止信令重放,缺点是每次都要去DAS获取新的签名.
- Linux 网络协议栈 图解
http://www.cnblogs.com/sammyliu/p/5225623.html
- 淘宝--印风 专注于MySQL内核代码
http://blog.csdn.net/zhaiwx1987/article/details/6113472?utm_source=jiancool
- CMMI之功能点估算法---内部逻辑文件和外部接口文件
功能点分析的步骤 在本文中将以国际标准IFPUG(International Function Point Users Group)组织提供的功能点估算法V4.1.1为基础与大家进行讲解.如下图所示, ...
- jQuery2.0应用开发:SSH框架整合jQuery2.0实战OA办公自己主动化(VSS、operamasks-UI框架)
我的qq是2059055336,对这个课程有兴趣的能够加我qq联系. 一.本课程是怎么样的一门课程(全面介绍) 1.1.课程的背景 jQuery 2.0 正式版公布.不在支持 IE 6/7/8 ...
- 北京市基本医疗保险A类定点医疗机构名单(2010-09-29)
1.中国医学科学院北京协和医院 2.首都医科大学附属北京同仁医院 3.首都医科大学宣武医院 4.首都医科大学附属北京友谊医院 5.北京大学第一医院 6.北京大学人民医院 7.北京大学第三医院 8.北京 ...
- cmake函数參数解析
近期在迁移公司的make系统到cmake上.发现cmake的function參数非常奇怪.比如,假设我们向一个function传递list作为參数,在function中,形參会变成例如以下状况: se ...
- HDU4183 起点到终点再到起点 除起点每点仅经过一次 网络流
题意: T个测试数据 n个圆 下面 fre x y r 表示圆的频率 坐标和半径 要求: 从频率为400(最小的) 圆 走到频率为789(最大)的圆,再走回来,除起点每个点只能经过一次 问这样的路径是 ...