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

#include<stdio.h>
#include<string.h>

int main()
{
    int dp[10000],time[10000],sum,cd[25];
    int N,m,t;
    while(scanf("%d%d",&N,&m)>0)
    {
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        while(m--)
        {
            scanf("%d",&t);
            for(int v = N; v>=t; v--)
                if(dp[v-t]&&dp[v]==0)
                dp[v]=dp[v-t]+1,time[v]=t;
        }
        for(sum=N;sum>0;sum--)
            if(dp[sum])
            break;
        int i=0;
        for(int j=sum;j>0;j=j-time[j])
            cd[++i]=time[j];

        for( ;i>0;i--)
        printf("%d ",cd[i]);
        printf("sum:%d\n",sum);
    }
}
时间: 2024-12-24 17:43:08

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背包)

//路径记录方法:若是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

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背包)

uva 624 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 ou

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

01背包专题

>>什么是01背包<< A - Bone Collector Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to co

hdoj 2126 Buy the souvenirs 【另类01背包】

题意:求最多购买的件数以及有几种方法. 一看到这题就想到了背包,因为求得是种类数,所以我们可以将件数看做价值,将价格看做重量,这就变成01背包了(dp),但是还要求有几种购买方案,那么再来一个背包(kind). 分析:有三种情况: 1>dp[j] < dp[j-s[i]]+1 那么对于这一种情况  方案背包的状态转移方程是kind[j] = kind[j-s[i]]?kind[j-s[i]]:1;(考虑到kind[j-s[i]] ==0的时候,这时候kind[j] = 1): 证明:为什么是k

HDU3810 Magina(搜索+用优先队列模拟01背包)经典

Magina Time Limit: 60000/30000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 528    Accepted Submission(s): 177 Problem Description Magina, also known as Anti-Mage, is a very cool hero in DotA (Defense of the Anci