UVA 624 ---CD 01背包路径输出
DescriptionCD

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<stdio.h>
#include<string.h> #define N 1100
#define max(a,b) (a>b?a:b) int v[N], dp[N][N], W, n; void Path(int n, int W)
{
if(n==) return ; if(dp[n-][W]==dp[n][W])
Path(n-, W);
else
{
Path(n-, W-v[n]);
printf("%d ", v[n]);
}
} int main()
{
while(scanf("%d%d", &W, &n)!=EOF)
{
int i, j;
memset(v, , sizeof(v));
memset(dp, , sizeof(dp));
for(i=; i<=n; i++)
scanf("%d", &v[i]); for(i=; i<=n; i++)
for(j=; j<=W; j++)
{
if(v[i]>j)
dp[i][j] = dp[i-][j];
else
dp[i][j] = max(dp[i-][j], dp[i-][j-v[i]]+v[i]);
} Path(n, W); printf("sum:%d\n", dp[n][W]);
}
return ;
}
一维数组:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<stack>
#include<vector>
#include<map>
using namespace std;
#define N 2510
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))
typedef long long LL; int n, m, a[N], dp[N], path[N][N]; int main()
{
while(scanf("%d %d", &m, &n)!=EOF)
{
met(dp, );
met(path, );
for(int i=; i<n; i++)
scanf("%d", &a[i]);
for(int i=; i<n; i++)
{
for(int j=m; j>=a[i]; j--)
{
///dp[j] = max(dp[j], dp[j-a[i]]+a[i]);
if(dp[j] < dp[j-a[i]]+a[i])
{
path[i][j] = ;
dp[j] = dp[j-a[i]]+a[i];
}
}
}
int j = m, k = , ans[N] = {};
for(int i=n-; i>=; i--)
{
if(path[i][j])
{
ans[k++] = a[i];
j -= a[i];
}
}
for(int i=k-; i>=; i--)
printf("%d ", ans[i]);
printf("sum:%d\n", dp[m]);
}
return ;
}
UVA 624 ---CD 01背包路径输出的更多相关文章
- UVA--624 CD(01背包+路径输出)
题目http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 624 - CD (01背包 + 打印物品)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- uva 624 CD 01背包打印路径
// 集训最终開始了.来到水题先 #include <cstdio> #include <cstring> #include <algorithm> #includ ...
- UVA 624 CD (01背包)
//路径记录方法:若是dp[j-value[i]]+value[i]>dp[j]说明拿了这个东西,标志为1, //for循环标志,发现是1,就打印出来,并把背包的容量减少,再在次容量中寻找标志: ...
- UVA 624 CD[【01背包】(输出路径)
<题目链接> 题目大意: 你要录制时间为N的带子,给你一张CD的不同时长的轨道,求总和不大于N的录制顺序 解题分析: 01背包问题,需要注意的是如何将路径输出. 由于dp时是会不断的将前面 ...
- 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 ...
- 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背包+路径的输出)
CD Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Practice UVA 624 ...
随机推荐
- ef中用lambda expressions时要注意(m=>m.id ==b ) 此时的b只能是基本的数据类型 。连属性都不能用
ef中用lambda expressions时要注意(m=>m.id ==b ) 此时的b只能是基本的数据类型 .连属性都不能用
- RAC DBCA 找不到共享磁盘
(一) 前言: 通过vmware workstation 走iscsi协议.安装RAC 集群架构,DBCA 时不能识别ASM 共享存储(按理来说这一版都是权限的问题).同一时候,本想通过RMAN ...
- sudo 之后 unable to resolve host的问题解决办法
gedit /etc/hosts #127.0.0.1 localhost #127.0.0.1 Masterback或者其他 把后面的Masterback 或者其他改成新的主机名,应该是最近修改过主 ...
- oracle当前月添加一列显示前几个月的累计值
create table test_leiji(rpt_month_id number(8), current_month NUMBER(12,2)); ...
- sql 字符串操作
SQL Server之字符串函数 以下所有例子均Studnet表为例: 计算字符串长度len()用来计算字符串的长度 select sname ,len(sname) from student ...
- MathType有哪些功能
随着寒流呼呼来临,期末考也不放过我们,不知道你们是在题海里遨游,还是已做好了挑战准备呢.不管你是哪种情况,现在就有一款玩转理工科的强大编辑公式强悍来袭,不管你是学生.教师,还是理科专业工作者,Math ...
- 【观点】“马云:金融是要为外行人服务",这个观点其实并不新鲜
不久前,马云在外滩国际金融峰会演讲,称金融行业需要“搅局者”.他说:“今天的金融,确实做得不错,没有今天这样的金融机构,中国的经济30年来不可能发展到今天,但是靠今天银行的机制,我不相信能支撑30年以 ...
- 通过phoenix创建hbase表失败,创建语句卡住,hbase-hmaster报错:exception=org.apache.hadoop.hbase.TableExistsException: SYNC_BUSINESS_INFO_BYDAY_EFFECT
问题描述: 前几天一个同事来说,通过phoenix创建表失败了,一直报表存在的错误,删除也报错,然后就针对这个问题找下解决方案. 问题分析: 1.通过phoenix创建表,一直卡住不动了.创建语句如下 ...
- 单行dp复习hdu1087
我写的想法是每个dp[i]都是前dp[i]的最大值 dp[i]就等于前全部dp[0...i-1]的最大值加上dp[i] 最大值是一个中间变量 最大值得选取条件就是序列的值大小都是递增的,也就是a[i] ...
- LeetCode——Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...