UVA 624CD(01背包输出 + 输出路径)
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
题意:求和最大的,弄了两个小时没鼓捣出来=_=
两种方法:一种是二维的dp[i][j] 表示前 i 种体积为 j 的最大价值量,那么对于 dp[i][j] = dp[i - 1][j]就一定不输出咯,因为这个表示第i个没选
第二种对于一维的来说:dp[i]表示 体积 为 i 的最大价值量,然后可以设一个数组来记录
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int Max = + ;
int trak[Max], dp[Max * ];
int vis[Max * ][Max];
int vistrak[Max];
int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
int traks;
scanf("%d", &traks);
for (int i = ; i <= traks; i++)
scanf("%d", &trak[i]);
memset(vis, , sizeof(vis));
memset(dp, , sizeof(dp));
memset(vistrak, , sizeof(vistrak));
for (int i = ; i <= traks; i++)
{
for (int j = n; j >= trak[i]; j--)
{
if (dp[j] <= dp[j - trak[i]] + trak[i])
{
dp[j] = dp[j - trak[i]] + trak[i];
vis[j][i] = trak[i]; // 体积为 j 时选 i 的价值
}
}
}
int tempn = n;
for (int i = traks; i > ; i--)
{
if (vis[tempn][i]) // 如果选了就应该是输出的,
{
vistrak[i] = ;
tempn -= vis[tempn][i];
}
}
for (int i = ; i <= traks; i++)
{
if (vistrak[i])
printf("%d ", trak[i]);
}
printf("sum:%d\n", dp[n]);
}
return ;
}
UVA 624CD(01背包输出 + 输出路径)的更多相关文章
- PAT L3-001 凑零钱(01背包dp记录路径)
韩梅梅喜欢满宇宙到处逛街.现在她逛到了一家火星店里,发现这家店有个特别的规矩:你可以用任何星球的硬币付钱,但是绝不找零,当然也不能欠债.韩梅梅手边有104枚来自各个星球的硬币,需要请你帮她盘算一下,是 ...
- 紫书 例题 9-5 UVa 12563 ( 01背包变形)
总的来说就是价值为1,时间因物品而变,同时注意要刚好取到的01背包 (1)时间方面.按照题意,每首歌的时间最多为t + w - 1,这里要注意. 同时记得最后要加入时间为678的一首歌曲 (2)这里因 ...
- UVa 12563 (01背包) Jin Ge Jin Qu hao
如此水的01背包,居然让我WA了七次. 开始理解错题意了,弄反了主次关系.总曲目最多是大前提,其次才是歌曲总时间最长. 题意: 在KTV房间里还剩t秒的时间,可以从n首喜爱的歌里面选出若干首(每首歌只 ...
- Jin Ge Jin Qu hao UVA - 12563 01背包
题目:题目链接 思路:由于t最大值其实只有180 * 50 + 678,可以直接当成01背包来做,需要考虑的量有两个,时间和歌曲数,其中歌曲优先级大于时间,于是我们将歌曲数作为背包收益,用时间作为背包 ...
- uva624 01背包要求输出路径
You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is o ...
- UVA 624(01背包记录路径)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 562(01背包)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=s ...
- Re0:DP学习之路 01背包如何打印路径?
伪代码 用二维数组记录,如果出现可以转移的dp那么记录bk[当前体积][装的物品]=1 输出的时候倒推,如果存在连通的边那么输出并且总共的体积减去输出的体积 代码(uva-624,目前wa不明所以,网 ...
- UVa 1213 (01背包变形) Sum of Different Primes
题意: 选择K个质数使它们的和为N,求总的方案数. 分析: 虽然知道推出来了转移方程, 但还是没把代码敲出来,可能基本功还是不够吧. d(i, j)表示i个素数的和为j的方案数,则 d(i, j) = ...
随机推荐
- shift粘滞键后门创建/复原批处理
创建shift粘滞键后门: 1 c: 2 3 cd \Windows\System32\ 4 5 rename sethc.exe bak_sethc.exe 6 7 xcopy cmd.exe se ...
- 为什么ReactiveCocoa中推荐使用RACSignal来做信号处理而不是RACSubject
原文解释在这里http://cocoadocs.org/docsets/ReactiveCocoa/0.6.0/ 在标题Creating hot subscribables 底下 先贴原文: The ...
- Mybatis环境
第一步:下载jar包并导入 1.mysql驱动包 2.mybatis环境包 第二步:创建MYSQL数据库 由于这是用于测试,只创建了test-usreinfo数据表 第三步:在src文件夹中创建myb ...
- Bundle包的制作与使用
一.清爽Bundle模式(在应用工程中创建Bundle的子文件夹,而非在Bundle项目中): 1.新建Bundle包 2.生成Bundle包,并拖入项目中,然后"右键显示包内容" ...
- 关于case语句中声明变量并初始化的注意事项
今天看到一句对这个问题特别精辟的总结,记录如下: It is possible to transfer into a block, but not in a way that bypasses dec ...
- Moq基础
一.概念 Moq是利用诸如Linq表达式树和Lambda表达式等·NET 3.5的特性,为·NET设计和开发的Mocking库.Mock字面意思即模拟,模拟对象的行为已达到欺骗目标(待测试对象)的效果 ...
- CentOS(5.8/6.7)linux生产环境若干优化实战
CentOS系统安装之后并不能立即投入生产环境使用,往往需要先经过我们运维人员的优化才行.在此讲解几点关于Linux系统安装后的基础优化操作.注意:本次优化都是基于CentOS(5.8/6.7). 下 ...
- Python的模块引用和查找路径
模块间相互独立相互引用是任何一种编程语言的基础能力.对于“模块”这个词在各种编程语言中或许是不同的,但我们可以简单认为一个程序文件是一个模块,文件里包含了类或者方法的定义.对于编译型的语言,比如C#中 ...
- windows中,端口查看&关闭进程及Kill使用
测试过程中遇到的问题,杂记一: 1.netstat -ano | findstr "8001" 查看端口8001被哪个进程占用:由下图可以看出,被进程为3736的占用 2.查 ...
- Apache:如何访问共享目录
环境说明:Apache的版本是2.4.10,共享目录有两种情况,一种是windows server的目录共享,还有一种是linux的NAS.无论访问哪一种共享目录,都需要用户名和密码. 问题说明:如何 ...