UVA 题目624 CD(01背包输出路径啊)
来源:互联网 发布:知行理工密码 编辑:程序博客网 时间:2024/05/18 12:40
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 minutesOutput
Set of tracks (and durations) which are the correct solutions and string `` sum:" and sum of duration times.Sample Input
5 3 1 3 410 4 9 8 4 220 4 10 5 7 490 8 10 23 1 2 3 4 5 745 8 4 10 44 43 12 9 8 2
Sample Output
1 4 sum:58 2 sum:1010 5 4 sum:1910 23 1 2 3 4 5 7 sum:554 10 12 9 8 2 sum:45
Miguel A. Revilla
2000-01-10
ac代码
#include<stdio.h>#include<iostream>#include<stdlib.h>#include<algorithm>#include<string.h>#include<math.h>#define INF 0x3f3f3f3f3f3f3f#define LL long longusing namespace std;LL a[1000100];LL dp[1000100];LL pre[1000100];LL now[1000100];LL ans[1000100];int n,m;void DP(){ int i,j; for(i=1;i<=n;i++) { for(j=m;j>=a[i];j--) { int temp=dp[j-a[i]]+a[i]; if(temp>dp[j]) { dp[j]=temp; now[dp[j]]=a[i]; pre[dp[j]]=dp[j-a[i]]; } } }}int main(){ //int n,m; while(scanf("%d%d",&m,&n)!=EOF) { int i,j; for(i=1;i<=n;i++) scanf("%lld",&a[i]); memset(dp,0,sizeof(dp)); memset(pre,-1,sizeof(pre)); memset(now,-1,sizeof(now)); DP(); LL x=dp[m]; int k=0; while(pre[x]!=-1) { ans[k++]=now[x]; x=pre[x]; } for(i=k-1;i>=0;i--) { //if(i) printf("%lld ",ans[i]); //else // printf("%d",ans[i]); } printf("sum:%lld\n",dp[m]); }}
0 0
- UVA 题目624 CD(01背包输出路径啊)
- UVa 624 CD (01背包路径输出)
- UVA 264 CD (01背包+输出路径)
- UVA 624 CD(01背包输出)
- uva 624 CD(01背包问题)输出最小字典序是个问题虽然题目不要求
- uva 624 CD (01背包+打印路径)
- UVA 624 - CD (01背包打印路径)
- CD - UVa 624 01背包记录路径
- uva 624 CD 01背包打印路径
- UVA 624 CD 01背包 输出解
- CD+uva+01背包(输出方案)
- UVA 624 CD ( 01背包 + 逆推路径)
- UVA - 624 CD(01背包+路径纪录)
- UVA 624 CD(01背包+dp打印路径)
- UVA 624 CD(01背包+记录路径)
- UVA 624 CD(01背包/记录路径)
- UVA 624 CD (01背包+打印路径)
- UVA 624CD(打印路径的01背包)
- 通过在BasicController使用ModelAttribute标签注入Request/Response/Session
- Struts2框架学习(二) Action
- 插入排序
- 有焦点的TextView
- 学习模式
- UVA 题目624 CD(01背包输出路径啊)
- 深度学习:对实际问题解决者的简要指导
- Android中View 绘制机制
- VS修改项目解决方案名称
- 《java并发编程实战》读书笔记——线程池
- HDOJ 题目1619 Unidirectional TSP(DP输出路径啊)
- 6大设计原则(一)---单一职责原则
- OC基础篇:day01&day02.
- 属性访问器