CD-----UVa624(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 这题主要是输出路径有问题
就是把路径记录下来
其他都好写 现在有两种方法
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<iostream>
using namespace std; int dp[],vis[][];
int f[],k=,w[]; void prime(int n,int m)
{
if(n==||m<)
return;
if(vis[n][m]==)
prime(n-,m);
else
{
prime(n-,m-w[n]);
f[k++]=w[n];;
}
}
int main()
{
int m,n,i,j;
while(scanf("%d",&m)!=EOF)
{
memset(w,,sizeof(w));
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d",&w[i]);
}
memset(dp,,sizeof(dp));
memset(vis,,sizeof(vis));
for(i=;i<=n;i++)
{
for(j=m;j>=w[i];j--)
{
if(dp[j]<dp[j-w[i]]+w[i])
{
dp[j]=dp[j-w[i]]+w[i];
vis[i][j]=;
}
}
}
k=;
memset(f,,sizeof(f));
prime(n,m);
for(i=;i<k;i++)
printf("%d ",f[i]);
printf("sum:%d\n",dp[m]);
}
return ;
}
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<iostream>
using namespace std; int dp[],vis[][];
int main()
{
int m,n,w[],i,j;
while(scanf("%d",&m)!=EOF)
{
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d",&w[i]);
}
memset(dp,,sizeof(dp));
memset(vis,,sizeof(vis));
for(i=n;i>0;i--)
{
for(j=m;j>=w[i];j--)
{
if(dp[j]<dp[j-w[i]]+w[i])
{
dp[j]=dp[j-w[i]]+w[i];
vis[i][j]=;
}
}
}
for(i=,j=dp[m];i<=n&&j>;i++)
{
if(vis[i][j])
{
printf("%d ",w[i]);
j=j-w[i];
}
}
printf("sum:%d\n",dp[m]);
}
return ;
}
CD-----UVa624(01背包+输出路径)的更多相关文章
- Codeforces Gym-102219 2019 ICPC Malaysia National E. Optimal Slots(01背包+输出路径)
题意:给你一个体积为\(T\)的背包,有\(n\)个物品,每个物品的价值和体积都是是\(a_{i}\),求放哪几个物品使得总价值最大,输出它们,并且输出价值的最大值. 题解:其实就是一个01背包输出路 ...
- UVA624(01背包记录路径)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA-624 CD---01背包+输出路径
题目链接: https://vjudge.net/problem/UVA-624 题目大意: 这道题给定一个时间上限,然后一个数字N,后面跟着N首歌的时间长度,要我们 求在规定时间w内每首歌都要完整的 ...
- vijos 1071 01背包+输出路径
描述 过年的时候,大人们最喜欢的活动,就是打牌了.xiaomengxian不会打牌,只好坐在一边看着. 这天,正当一群人打牌打得起劲的时候,突然有人喊道:“这副牌少了几张!”众人一数,果然是少了.于是 ...
- UVA624 CD,01背包+打印路径,好题!
624 - CD 题意:一段n分钟的路程,磁带里有m首歌,每首歌有一个时间,求最多能听多少分钟的歌,并求出是拿几首歌. 思路:如果是求时常,直接用01背包即可,但设计到打印路径这里就用一个二维数组标记 ...
- UVA 624 CD[【01背包】(输出路径)
<题目链接> 题目大意: 你要录制时间为N的带子,给你一张CD的不同时长的轨道,求总和不大于N的录制顺序 解题分析: 01背包问题,需要注意的是如何将路径输出. 由于dp时是会不断的将前面 ...
- UVA 624 CD(01背包,要记录路径)
题意: 有n张CD(n<=20),每张能播放的时长不同.给定一个时长限制t,挑出部分的CD使得总播放时间最长.顺便输出路径! 思路: 重点在输出路径,否则这题很普通.那就要用二维数组记录每个CD ...
- 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 ...
随机推荐
- CSV解析
NSString *path = [[NSBundle mainBundle] pathForResource: @"type" ofType:@"csv"]; ...
- ZigBee cc2530芯片学习 error记录(1)
ZigBee cc2530芯片学习 error记录 Error[e46]: Undefined external "LcdInit" referred in main( xxx ...
- SEO 第十章
SEO第十章 本次课目标: 1. 站外优化方案计划 2. 常见的SEO作弊手段(黑帽) 3. 百度站长平台的使用 4. 网站流量提升和转化率提升 一.站外优化方案计划 友情链接 权重相当.行业 ...
- github 下载全部项目
从github下载资料过程中,有些项目含有子模块,有时通过git clone 或者下载zip方式项目可能会缺少文件,因此需要执行 git submodule update --init --recur ...
- sqlit3事务
事务定义了一组SQL命令的边界,这组命令或者作为一个整体被全部执行,或者都不执行.事务的典型实例是转帐. 事务的范围 事务由3个命令控制:BEGIN.COMMIT和ROLLBACK.BEGIN开始一个 ...
- PHP01 LAMP网站构建
学习要点 什么是web? 开发动态网站所需的web构件? 几种主流web应用程序平台? HTTP协议与web的关系? Web的工作原理? LAMP网站开发组合概述? 如何学习PHP? 什么是Web? ...
- vscode 中文设置
修改设置 语言设置介绍: https://code.visualstudio.com/docs/getstarted/locales 按Ctrl + Shift + P打开命令调色板,然后开始键入“d ...
- Linux 特殊指令总结(持续更新)
Linux 命令 1. 查看系统信息 1.uname uname (1) - print system information uname (2) - get name and information ...
- selenium 浏览器基础操作(Python)
想要开始测试,首先要清楚测试什么浏览器.如何为浏览器安装驱动,前面已经聊过. 其次要清楚如何打开浏览器,这一点,在前面的代码中,也体现过,但是并未深究.下面就来聊一聊对浏览器操作的那些事儿. from ...
- 关于Python中包裹传参和解包裹的理解
1.包裹传参 首先思考一个问题:为什么要有包裹传参?原因包括但不仅限于以下两点:①不确定参数的个数.②希望函数定义的更加松散灵活 包裹传参分两种:包裹位置传参和包裹关键字传参.先看包裹位置传参: 在这 ...