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 ...
随机推荐
- Go语言Web框架gwk介绍 (三)
上一篇忘了ChanResult ChanResult 可以用来模拟BigPipe,定义如下 type ChanResult struct { Wait sync.WaitGroup Chan chan ...
- COM/DCOM开发练习之进程内组件实例
作者 : 卿笃军 题目说明: 仿照例题,在其基础上实现下面功能: 1)使用C++语言实现进程内组件,组件提供复数的加.减.乘.除等计算服务:client部分包含录入(实部和虚部分开录入)和查询部分. ...
- The YubiKey NEO -- Smartcard features
Smartcard features on the YubiKey NEO YubiKeys are a line of small and low-cost hardware security to ...
- mysql slock
http://www.itdks.com/dakashuo/new/dakalive/detail/3888
- startActivities的使用
和startActivity()类似,startActivities也是界面跳转,可是传入的intent是一个数组,也就是说是多个. 如果我传入的是两个intent: I1和I2.则调用startAc ...
- UVALive 3135--Argus+自己定义优先队列的优先规则
题目链接:id=18684">点击进入 仅仅是题意比較难懂,读懂题后全然能够用优先队列水过去.这次学会自己定义优先队列的优先规则,事实上就是在结构体中重载一下<运算符. 代码例如 ...
- 转: gob编解码
要让数据对象能在网络上传输或存储,我们需要进行编码和解码.现在比较流行的编码方式有JSON,XML等.然而,Go在gob包中为我们提供了另一种方式,该方式编解码效率高于JSON.gob是Golang包 ...
- 借助 Resharper 和 StyleCop 让代码更整洁
一:工具安装 Resharper 和 StyleCop 必须安装. Resharper 的配置文件如下:Resharper.zip 请按如下步骤导入, 1: 2: 3: StyleCope 的配置 ...
- 【BZOJ】【3653】谈笑风生
dfs序+可持久化线段树 好吧……是我too naive 这题……$$ans=min(dep[x],k)×(size[x]-1)+\sum_{y在x的子树中,且dis(x,y)<=k}(size ...
- 用Java操纵HBase数据库(新建表,插入,删除,查找)
java代码如下: package db.insert; /* * 创建一个students表,并进行相关操作 */ import java.io.IOException; import java.i ...