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 ...
随机推荐
- Codeforces Round #355 (Div. 2) A. Vanya and Fence 水题
A. Vanya and Fence 题目连接: http://www.codeforces.com/contest/677/problem/A Description Vanya and his f ...
- hdu 4442 Physical Examination 贪心排序
Physical Examination Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- C# 高级编程9 第30章MEF C#可扩展编程之MEF第一章
MEF(Managed Extensibility Framework)是一个用于创建可扩展的轻型应用程序的库 利用该库轻松地封装代码,避免生成脆弱的硬依赖项. 通过 MEF,不仅可以在应用程序内重用 ...
- Android中播放本地SD卡中歌曲须要的加入的权限
使用MediaPlayer播放本地Mp3文件时.须要注意的訪问路径的问题以及訪问权限的问题. 1.訪问路径:/storage/emulated/0 此路径即为手机的根路径,能够通过下载ES文件浏览器软 ...
- FolderSync :The various features and how to use them
Help - Tacit Dynamics Help This page describes the various features of FolderSync and how to use the ...
- How to check Ubuntu version
Below you can find some tips on how to check Ubuntu version you are currently running. The first pla ...
- 百度地图api改变覆盖物背景实例及css颜色值简介
在此鸣谢buptwusuopu的技术支持 在调用百度地图api的时候,为了改变覆盖物的颜色,如图中椭圆型的填充色.可以到百度api的库中查找方法http://developer.baidu.com/m ...
- linux驱动移植的重要数据结构
转载:http://www.embeddedlinux.org.cn/html/jishuzixun/201304/14-2538.html 对于嵌入式 Linux 系统来说,有各种体系结构的处理器和 ...
- C#程序集系列06,程序集清单,EXE和DLL的区别
CLR在加载程序集的时候会查看程序集清单,程序集清单包含哪些内容呢?可执行文件和程序集有什么区别/ 程序集清单 □ 查看程序集清单 →清空F盘as文件夹中的所有内容→创建MainClass.cs文件→ ...
- tomcat 启动时设置 java 参数,mark
在文件 startup.bat/.sh 中添加 set "JAVA_OPTS=-Xms2048m -Xmx4096m -XX:NewSize=256m -XX:MaxNewSize=102 ...