uva 624 CD (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 简单的01背包,不过要输出路径,第一次写感觉特别麻烦,建立二维数组记录是否选中此物体。 题意:第一个数是路程,第二个数是歌曲的个数,后面的数是听每首歌的时间。要求在这段时间上尽可能地长时间听歌,每首歌必须听完整,总共最大能听歌的时间长度。
输出所有听的歌曲的时间,顺序为输入的顺序,以及最后总共的时间。 附上代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int maxs(int a,int b)
{
return a>b?a:b;
}
int main()
{
int i,j,n,m;
int a[],dp[];
int s[][];
while(~scanf("%d %d",&m,&n))
{
for(i=; i<n; i++)
scanf("%d",&a[i]);
memset(dp,,sizeof(dp));
memset(s,,sizeof(s));
for(i=n-; i>=; i--) //反序输入,为了后面能用正序
for(j=m; j>=a[i]; j--) //最简单的01背包
if(dp[j]<dp[j-a[i]]+a[i])
{
// dp[j]=maxs(dp[j],dp[j-a[i]]+a[i]); //这样写会报错!!!我也不知道为什么= = 可能是电脑的问题,纠结了一下午
dp[j]=dp[j-a[i]]+a[i];
s[i][j] =;
}
for(i=,j=dp[m]; i<n,j>; i++) //输出路径
{
if(s[i][j])
{
printf("%d ",a[i]);
j-=a[i];
}
}
printf("sum:%d\n",dp[m]);
}
return ;
}
uva 624 CD (01背包)的更多相关文章
- UVA 624 - CD (01背包 + 打印物品)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 624 ---CD 01背包路径输出
DescriptionCD You have a long drive by car ahead. You have a tape recorder, but unfortunately your b ...
- UVA 624 CD (01背包)
//路径记录方法:若是dp[j-value[i]]+value[i]>dp[j]说明拿了这个东西,标志为1, //for循环标志,发现是1,就打印出来,并把背包的容量减少,再在次容量中寻找标志: ...
- uva 624 CD 01背包打印路径
// 集训最终開始了.来到水题先 #include <cstdio> #include <cstring> #include <algorithm> #includ ...
- UVA 624 CD(01背包+输出方案)
01背包,由于要输出方案,所以还要在dp的同时,保存一下路径. #include <iostream> #include <stdio.h> #include <stri ...
- UVA 624 CD(DP + 01背包)
CD You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music i ...
- UVA624 CD,01背包+打印路径,好题!
624 - CD 题意:一段n分钟的路程,磁带里有m首歌,每首歌有一个时间,求最多能听多少分钟的歌,并求出是拿几首歌. 思路:如果是求时常,直接用01背包即可,但设计到打印路径这里就用一个二维数组标记 ...
- CD(01背包)
You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is o ...
- 紫书 习题 10-5 UVa 1213(01背包变形)
这里就是01背包多了一维物品个数罢了 记得不能重复所以有一层循环顺序要倒着来 边界f[0][0] = 1 #include<cstdio> #include<vector> # ...
随机推荐
- Linux操作系统各版本ISO镜像下载(包括oracle linux\redhat\centos\u
Linux操作系统各版本ISO镜像下载(包括oracle linux\redhat\centos\ubuntu\debian等) 1.Oracle Linux(下载地址) (1)OracleLinux ...
- Codeforces Round #416 (Div. 2) A. Vladik and Courtesy【思维/模拟】
A. Vladik and Courtesy time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 关于如何在部署环境修改process.env & 本地测试
前言 最近在搞一些监控的东西, 需要根据不同的环境上报到不同的地址,中间遇到了一些问题,在这里简单总结分享下. 关于process.env 假如你对process.env 相关的概念还不熟悉, 请移步 ...
- 洛谷 P1858 多人背包 DP
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 题面 题目链接 洛谷 P1858 多人背包 题目描述 求01背包前k优解的价值 ...
- 深入浅出Cocoa 之动态创建类【转】
在前文<深入浅出Cocoa之类与对象>一文中,我已经详细介绍了ObjC中的 Class 与 Object 的概念,今天我们来如何在运行 时动态创建类.下面这个函数就是应用前面讲到的Clas ...
- [框架]eclipse搭建ssm框架 一 标签: eclipsetomcat框架 2017-03-25 21:28 1085人阅读 评论(
虽然现在也做过一些项目,但是自己从头搭起来的框架几乎没有,所以这两天自己搭了一下ssm的框架,下面写一下框架的搭建过程.并且给出增删改查四条线来方便大家熟悉代码. 环境准备 maven3.2.3 ec ...
- 【JZOJ1922】【Usaco 2005 NOV Gold】小行星群
题目描述 Bessie想驾驶她的飞船穿过危险的小行星群,小行星群是一个N×N的网格(1 <= N <= 500),在网格内有K个小行星(1 <= K <= 10,000). 幸 ...
- 智能算法之Matlab实现(1)——遗传算法(1)
遗传算法的过程在这里先不介绍了,可能在接下来的几篇文章会介绍,这里介绍些实用的. (1)Sheffield遗传算法工具箱的安装 我共享了下修改过文件名和后缀名的原版工具箱,地址为:http://pan ...
- IDEA入门(1)--lombok和Junit generator2插件的运用
前言 最近在慕课网看到了一些视频,准备从0开始做一个电商网站.视频中的大牛用的java的IDE都是IDEA,让我很纠结.从as到MyEclipse,好不容易稍微熟悉了一下MyEclipse的基本操作, ...
- 两张图说明http协议,tcp协议,ip协议,dns服务之间的关系和区别
一.理解一个传输流再去扩展 用http举例来说,首先作为发送端的客户端在应用层(http协议)发出一个想看某个web页面的http请求. 接着,为了传输方便,在传输层(tcp协议)把从应用层处收到的数 ...