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> # ...
随机推荐
- 模拟退火解TSP问题MATLAB代码
分别把前四个函数存成m文件,再运行最后一个. swap.m function [ newpath , position ] = swap( oldpath , number ) % 对 oldpath ...
- pytest fixture 利用 params参数实现用例集合
@pytest.fixture有一个params参数,接受一个列表,列表中每个数据都可以作为用例的输入.也就说有多少数据,就会形成多少用例.如下面例子,就形成3条用例 test_parametrizi ...
- 51nod1040 矩阵相乘结果的判断
给出三个N*N的矩阵A, B, C,问A * B是否等于C?Input第1行,1个数N.(0 <= N <= 500)第2 - N + 1行:每行N个数,对应矩阵A的元素.(0 <= ...
- TRS OM error
http://192.168.1.1/ http://tplogin.cn/admin888 wddqaz123456789 package="com.trs.om.bean" m ...
- 【OI】位运算操作
一.基础操作 1.a<<b 将二进制a左移b位,不够的地方用0补位 例如 100<<2 == 10000 2.a>>b 将二进制a右移b位 例如 100>&g ...
- rsa加解密的内容超长的问题解决
一. 现象: 有一段老代码用来加密的,但是在使用key A的时候,抛出了异常:javax.crypto.IllegalBlockSizeException: Data must not be ...
- 用Java来获取访问者真实的IP地址
用Java来获取访问者真实的IP地址 转载 2016年06月07日 14:36:02 标签: 16497 编辑 删除 在JSP里,获取客户端的IP地址的方法是:request.getRemoteAdd ...
- AtCoder Beginner Contest 064 D - Insertion
AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...
- eclipse Some projects cannot be imported because they already exist in the workspace
archive file 档案文件 删除对应的文件即可
- 解决eclipse不会自动弹出Console控制台的问题
有时候Eclipse启动,控制台console不会自动跳出来,需要手工点击该选项卡才行, 解决方案: windows -> preferences -> run/debug ...