uva624 CD (01背包+路径的输出)

CD

Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu

Submit Status Practice UVA 624

Appoint description:

Description

Download as PDF

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

Miguel A. Revilla

2000-01-10

//把题目转化为背包问题。即选择尽量多的CD曲子去装满这个时间区间
#include<iostream>
#include<algorithm>
#include<map>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<cstring>
#include<stack>
#include<string>
#include<fstream>
#define pb(s) push_back(s)
#define cl(a,b) memset(a,b,sizeof(a))
#define bug printf("===\n");
using namespace std;
typedef vector<int> VI;
#define rep(a,b) for(int i=a;i<b;i++)
#define rep_(a,b) for(int i=a;i<=b;i++)
#define P pair<int,int>
#define bug printf("===\n");
#define PL(x) push_back(x)
#define X first
#define Y second
#define vi vector<int>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define rep_(i,x,n) for(int i=x;i<=n;i++)
const int maxn=15000;
const int inf=999999999;
typedef long long LL;
int a[maxn];
int dp[maxn];
int f[maxn][maxn];
int cnt;
int ans[maxn];
void print(int n,int m){//是dp的逆过程。递归回去
    if(m==0)return ;
    if(f[m][n]){
        print(n-a[m],m-1);
        ans[cnt++]=a[m];
    }
    else {
        print(n,m-1);
    }
}
int main(){
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        for(int i=1;i<=m;i++){
            scanf("%d",&a[i]);
        }
       for(int i=0;i<=n;i++){
            dp[i]=0;
            for(int j=0;j<=n;j++){
                f[i][j]=0;
            }
       }
        for(int i=1;i<=m;i++){
            for(int j=n;j>=a[i];j--){
                dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
                if(dp[j]==dp[j-a[i]]+a[i]){
                    f[i][j]=1;
                }
            }
        }
        cnt=0;
        print(n,m);
        for(int i=0;i<cnt;i++){
            printf("%d ",ans[i] );
        }
        printf("sum:%d\n",dp[n]);
    }
    return 0;
}
时间: 2024-10-02 20:30:52

uva624 CD (01背包+路径的输出)的相关文章

UVa CD 0-1背包且打印路径

就是简单的0-1背包问题,不过没有具体的效益值,隐含的效益值就是剩余背包的容量.因为要输出具体选择了那些track(也就是物品),所以采用序偶的方法.其实0-1背包的解画在坐标轴上就是一个分段函数,所谓序偶就是那些跃迁的节点.但是这道题略有不同,第0阶段的初始序偶不是(0,0),而是(0,N).序偶的第一个参数表示容量,第二个参数表示背包的剩余容量.当由前一阶段的序偶得到新序偶,并且将两者合并的时候.应当按照序偶的第一个参数从小到大进行合并,如果下一个要合并的序偶的剩余容量大于当前最后一个以合并

UVA 624 CD (01背包+打印路径 或 dfs+记录路径)

Description 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 o

uva 624 CD 01背包打印路径

// 集训最终開始了.来到水题先 #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; int a[23]; int d[23][100000]; int flag[23]; int W,n; void init(){ cin >> n; for (int i=1;i<=n;i++) cin >

UVA 624 CD (01背包)

//路径记录方法:若是dp[j-value[i]]+value[i]>dp[j]说明拿了这个东西,标志为1, //for循环标志,发现是1,就打印出来,并把背包的容量减少,再在次容量中寻找标志: #include <iostream> #include <cstring> #include <algorithm> using namespace std; int value[30],dp[10001],s[30][10001]; int main() { int

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 t

vijos 1071 01背包+输出路径

描述 过年的时候,大人们最喜欢的活动,就是打牌了.xiaomengxian不会打牌,只好坐在一边看着. 这天,正当一群人打牌打得起劲的时候,突然有人喊道:“这副牌少了几张!”众人一数,果然是少了.于是这副牌的主人得意地说:“这是一幅特制的牌,我知道整副牌每一张的重量.只要我们称一下剩下的牌的总重量,就能知道少了哪些牌了.”大家都觉得这个办法不错,于是称出剩下的牌的总重量,开始计算少了哪些牌.由于数据量比较大,过了不久,大家都算得头晕了. 这时,xiaomengxian大声说:“你们看我的吧!”于

UVA 624 CD (01背包 带路径)

题意 输入两个数 len,n 表示长度和个数,接下来输入n个数, 表示每一个的长度, 求这n个数能够组成的不超过len的最大长度,并输出这些数. 分析:01背包,dp数组非0表示可以组成的数,dp数组用来记录路径 #include <iostream> #include <queue> #include <cstdio> #include <cstring> #include <cstdlib> #include <stack> #i

UVA624(01背包记录路径)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=565 01背包打印路径. #include <cstdio> #include <cstring> using namespace std; const int MAXN=10005; int n,W; int w[30]; int dp[MAXN]; in

0-1背包打印路径(递归和非递归版本)

简单的0-1背包打印路径问题,我们可以记录一个p[][]数组来判断,当前物品是否被选中,最后按照记录输出,注意是逆序. #include<stdio.h> #include<string.h> int main() { int a[25],p[25][10005],i,j,n,m,s[10005]; while(scanf("%d%d",&m,&n)!=EOF){ for(i=1;i<=n;i++) scanf("%d"