UVA 624 ---CD 01背包路径输出
DescriptionCD
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<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 ...
随机推荐
- 今天搞log4net插入错误日志去mysql数据库的时候出现了点问题,已解决。记录下解决方案
先上图 配置log4net的时候要填这项,可是这个value我不知道啊.....上图里的value是我用下面的方法获取的 MySqlConnection con = new MySqlConnecti ...
- Win10開始菜单打不开
一.前言 自从用Win10之后(附上<我的Win10之旅>).用清理软件.总是深度清理,导致rt问题. 每次百度都是没用的解决方法: 今天,再一次清理(Wise Care 365 注冊表深 ...
- android 沉浸式状态栏(像ios那样的状态栏与应用统一颜色样式)
这个特性是andorid4.4支持的,最少要api19才干够使用.以下介绍一下使用的方法,很得简单: 添加一个demo源代码: https://github.com/ws123/StatusDemo ...
- linux配置java环境
第一步: vim /etc/profile 第二步:添加以下4行 第三步:使配置生效 source /etc/profile 结束配置
- 什么是mysql的元数据呢?
需求描述: 什么是mysql的元数据?metadata? 概念解释: 简单来说:任何描述数据库的数据就是数据库的元数据库(与数据库中存在的内容相对) 比如,用于描述一个冰箱是如何构成,怎么使用的就是冰 ...
- c#接口作为参数传递、返回
接口做为参数传递,传递的是实现了接口的对象: 接口作为类型返回,返回的是实现了接口的对象. 接口的传递与返回就是围绕着上面的两句话展开的.
- python2.0_day18_django_admin
Django admin的个性化定制首先我们看下,前面章节中定义的models在admin后台管理界面的样子: 然后我们看下老男孩教育点名平台的admin管理表的后台界面样子: admin管理后台常用 ...
- nodejs搭建简单web服务器!!
var http = require("http"), url = require("url"), path = require("path" ...
- 常用hive的CLI命令
1.show tables --查看所有表 2.desc tabname --查看表信息 3.dfs -ls 目录 查看hdfs上面的文件 dfs -lsr /user 递归显示目录/user ...
- html的初识
今天我们学习了Html语言,感觉学习这个是我期望很久的啦,之前在百度上面也看过html教程,但是看过之后也忘记啦,太多需要记忆的,所以也没记得什么啦.甚是遗憾啊,总感觉html需要学习好多东西啦的,但 ...