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 ...
随机推荐
- 基于mvcpager的分页(get请求,刷新页面),提供两种样式(来自bootstrap的样式)
使用方法:先把mvcpager.dll引用加入mvc项目 下载路径在本文末尾 前台代码 前台: @{ Layout = null; } @using Webdiyer.WebControls.Mvc ...
- Hibernate学习(1):查询demo
1.数据库(mysql)创建脚本 DROP TABLE IF EXISTS role; CREATE TABLE IF NOT EXISTS `role`( `id` ) NOT NULL AUTO_ ...
- HBase源代码分析之MemStore的flush发起时机、推断条件等详情
前面的几篇文章.我们具体介绍了HBase中HRegion上MemStore的flsuh流程,以及HRegionServer上MemStore的flush处理流程.那么,flush究竟是在什么情况下触发 ...
- 如何用ChemDraw建立多中心结构
通过调整ChemDraw多中心机构的连接可绘制有意义的络合物结构,建立中心原子和络合配体后,利用多中心化学键连接上述结构即可.以下内容将具体介绍如何用ChemDraw建立多中心结构. 一.多中心键和多 ...
- redis客户端使用密码
./redis-cli -h 127.0.0.1 -p 6379 -a password
- hadoop基本认识
还是hadoop专有名词进行说明. Hadoop框架中最核心设计就是:HDFS和MapReduce.还有yarn HDFS提供了海量数据的存储.(分布式文件系统) MapReduce提供了对数据的计算 ...
- redis安装之zmalloc.h:55:2: error: #error "Newer version of jemalloc required"错误
redis是C语言编写的软件,安装前需要编译,需要gcc编译环境,确认安装gcc编译环境后(安装gcc命令:yum install gcc-c++) 在redis解压目录下,确认有Makefile文件 ...
- Python 使用正则表达式匹配URL网址
使用正则表达式匹配以 .com 或 .cn 为域名后缀的URL地址 In [1]: import re In [2]: str = "http://www.baidu.com/" ...
- UART简介
经常遇到初学者,对单片机串行通讯出了问题不知道如何办的情况.其实最有效的调试方法是用示波器观察收发数据的波形.通过观察波形可以确定以下情况: 1.数据是否接收或发送: 2.数据是否正确: 3.波特率是 ...
- COM组件技术名称解释
GUID:全局唯一标识. CLSID 或 ProgID :唯一地表示一个组件服务程序,那么根据这些ID,就可以加载运行组件,并为客户端程序提供服务了. IID :唯一的表示接口ID. COM 组件是运 ...