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) = ...
随机推荐
- SharePoint Server2016的User Profile Services服务
前言SharePoint Server 的早期版本具有内置的 ForeFront Identity Manager (FIM) 副本,可在 SharePoint Server 服务器产品内运行.具 ...
- UITabBarController 基本定制
UITabBarController 定制 特点 用法 1.准备好你的tabBar图片及其他图片(哈哈哈!!!!),我的图片都放在了Assets.xcassets中. 2.导入本工程中的Categro ...
- Android中点击隐藏软键盘最佳方法——Android开发之路4
Android中点击隐藏软键盘最佳方法 实现功能:点击EditText,软键盘出现并且不会隐藏,点击或者触摸EditText以外的其他任何区域,软键盘被隐藏: 1.重写dispatchTouchEve ...
- SE Springer小组之《Spring音乐播放器》可行性研究报告三、四
3 对现有系统的分析 由于本次可行性分析主要是建立在团队自行实现一个音乐软件的目标上,并不是在一个现有系统的基础上开发改进的新系统.因此这里将分析一款市面上已经存在的音乐软件(以下称为W音乐),并为之 ...
- iOS系列 基础篇 01 构建HelloWorld,剖析并真机测试
iOS基础 01 构建HelloWorld,剖析并真机测试 前言: 从控制台输出HelloWorld是我们学习各种语言的第一步,也是我们人生中非常重要的一步. 多年之后,我希望我们仍能怀有学习上进的心 ...
- 一种面向对象的TCP/IP中间件
这是一个使用C++封装的TCP/IP协议栈(仅传输层),属于本人所设计的中间件的一员,具有硬件无关,应用无关特性,使用非常方便,一看代码便知: #include "net.h" / ...
- pcDuino-V2利用madplay播放音乐
在pcDuino的UBUNTU系统下,打开控制台,利用apt-get来下载madplay软件. sudo apt-get install madplay 播放音乐: madplay xxx.mp3 x ...
- 深入C#编程
1.NET框架 1.1.NET框架结构 VB.NET C# F# lronruby others.... .NET FrameWork(FCL) 公共语言进行时(CLR) 操作系统 .NET框架具有两 ...
- Docker 简介
1.什么是Docker Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux 机器上.使用Docker可以让每个应用彼此相 ...
- C# 7.0 新功能代码范例
随着新版本的IDE Visual Studio 15 紧锣密鼓的开发中,2016年8月24日,微软发布了与之配套的C# 7.0 preview 的新特性. 其主要特性有: 内联声明out变量 (Out ...