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 ...
随机推荐
- DOM-XML(解析与创建)
/** * 读取(解析)xml文件 * @author Administrator * */ public class DOMRead { public static void main(String ...
- 活动(Activity)
一.用Log打印日志 Log.d("HelloWorldActivity", "onCreate execute"); 二.Toast用法 Toast.make ...
- Redis哨兵模式主从同步不可以绑定127.0.0.1或者0.0.0.0,不然无法进行主从同步
Redis哨兵模式主从同步不可以绑定127.0.0.1或者0.0.0.0,不然无法进行主从同步,一定要绑定内网IP,而对于跨机房的问题,可以使用iptables进行nat转发来解决.
- Content portal for Pocketables Tasker articles
http://www.pocketables.com/2013/03/overview-of-pocketables-tasker-articles.html I write a lot about ...
- Windows Phone Silverlight 8.1 apps
The Windows Phone Silverlight 8.1 app model gives Windows Phone 8 developers access to some of the n ...
- 报错:对一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性
当使用Entity Framework,并且把领域模型的某个字段设置成不能为null,但在保存.更新的时候,如果领域模型的该字段为null了,就会报如上错误. 解决方法:给领域模型不能为null的字段 ...
- 使用jdbc的缺点
使用jdbc开发时有以下缺点 1,数据库连接,使用时就创建,不使用就释放,对数据库进行频繁连接开关和关闭,造成数据库资源浪费,影响数据库的性能 解决:使用数据库连接池管理数据库的连接 2,sql语句使 ...
- python3 urllib.request 网络请求操作
python3 urllib.request 网络请求操作 基本的网络请求示例 ''' Created on 2014年4月22日 @author: dev.keke@gmail.com ''' im ...
- vld,Bounds Checker,memwatch,mtrace,valgrind,debug_new几种内存泄露检测工具的比较,Valgrind Cheatsheet
概述 内存泄漏(memory leak)指由于疏忽或错误造成程序未能释放已经不再使用的内存的情况,在大型的.复杂的应用程序中,内存泄漏是常见的问题.当以前分配的一片内存不再需要使用或无法访问时,但是却 ...
- 卷积神经网络用于视觉识别Convolutional Neural Networks for Visual Recognition
Table of Contents: Architecture Overview ConvNet Layers Convolutional Layer Pooling Layer Normalizat ...