UVA_624 CD

CD
UVA 624
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背包,但是需要保存路径输出。注意题目不要求填满背包,只需要占据时间尽量长。
因为题目没有给出N的取值范围,所以最开始的时候定义dp[]:挑选到第i张碟的最长时间,递推式:dp[i]=max(dp[i],dp[j]+T[i]);(0< =j< i)。但是这中算法其实是错误的,比如数据:11 3 4 5 6.我们只能记录到当前的最大值,但是不能涵盖所有的可达点。
按照基本的01背包,我们定义dp[i]:可达时间为i时对应的时间T[i];这样方便我们最后的逆序输出。关键在于输出的方式。(对于背包的保存路径输出留作以后碰到再做整理)附上代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#define MAX_N 22
#define MAX_T 10000
using namespace std;

//时间能达到i的最后一个时间段
int dp[MAX_T];
int T[MAX_N];
int N,num,result;
void print(int x);
int main()
{
    while( scanf("%d%d",&N,&num) != EOF )
    {
        result=0;
        memset(dp,-1,sizeof(dp));
        for( int i = 0; i < num; i++ )
            scanf("%d",&T[i]);
        dp[0]=0;
        for( int i = 0; i < num; i++ )
        {
            for( int j = N; j >= T[i]; j-- )
            {
                if( dp[j-T[i]]!=-1 &&dp[j] == -1 )
                {
                    dp[j]=T[i];
                    result=max(result,j);
                }
            }
        }
        print(result);
        printf("sum:%d\n",result);
    }
}
void print(int x)
{
    if( x <= 0 )
        return ;
    print(x-dp[x]);
    printf("%d ",dp[x]);
    return ;
}

另外几种输出方法:

for(int i = m-1; i >= 0; --i){
            for(int j = n; j >= arr[i]; --j){
                if(num[j-arr[i]] != -1 && num[j] == -1 ){
                    num[j] =arr[i];
                }
            }
        }

        while(num[n] == -1){
            n--;
        }
        int ans = n;
        while(n){
            printf("%d ", num[n]);
            n -= num[n];
        }
 for (j = vol; j >= track[i]; j --) {
                if(dp[j-track[i]] + track[i] > dp[j]) {
                    dp[j] = dp[j-track[i]] + track[i];
                    vis[i][j] = 1;
                }
            }
        }
        for (int i = n, j = vol; i >= 0; i --) {
            if(vis[i][j]) {
                printf("%d ", track[i]);
                j -= track[i];
            }
        }
        for(int i = 0; i < m; ++i){
            for(int j = n; j >= arr[i]; --j){
                if(dp[j-arr[i]] != -1 && dp[j] == -1){
                    dp[j] = arr[i];
                }
            }
        }
        int tmp = n;
        while(dp[tmp] == -1){
            tmp--;
        }
        int ans = tmp;
        stack<int> s;
        while(dp[tmp] != -2){
//            printf("")
//            printf("%d ", dp[tmp]);
            s.push(dp[tmp]);
            tmp -= dp[tmp];
        }
        while(!s.empty()){
            printf("%d ", s.top());
            s.pop();
        }
        printf("sum:%d\n", ans);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值