UVA 624 记录路径的0-1背包

题意:
一段很长的旅程,你有 一个 N 分钟的磁带,但是你喜欢的歌曲都在CD里 ,
你需要把CD的歌曲都录进磁带,问放哪些歌曲可以尽可能的利用磁带空间。
输入 磁带容量 N 和 歌曲数量 x ,接下来 x 个数表示每首歌曲的时长。
输出是哪些歌曲,和总时长。

解题:

0-1背包呀,就可以求出来最多可以放多少首进去了。
然后另外用一个数组 记录 背包容量为 j 时放进了第几件物品,就可以输出辣。
最先数组不够大 RE 辣

2333虽然简单题但是自己写出来还是很兴奋啊

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int maxn = 100010;
const int INF = 0x3f3f3f3f;

int d[maxn],a[100],p[maxn];

int main()
{
    int n, num;
    while (scanf ("%d", &n) != EOF) {
        scanf ("%d", &num);
        for (int i = 1; i <= num; i ++) {
            scanf ("%d", &a[i]);
        }
        int d[maxn] = {0}, p[maxn] = {0};
        for (int i = 1; i <= num; i ++) {
            for (int j = n; j >= a[i]; j --) {
                if(d[j-a[i]] + a[i] > d[j]) {
                    d[j] = d[j-a[i]] + a[i];
                    p[j] = i;
                }
            }
        }
        int i = d[n], cnt = 0,ans[100] = {0};
        while (i > 0) {
            ans[cnt++] = a[p[i]];
            i = i - a[p[i]];
        }
        for (int j = cnt-1; j >= 0; j --)
            printf ("%d ",ans[j]);
        printf ("sum:%d\n",d[n]);
    }
    return 0;
}
时间: 2024-11-07 22:03:49

UVA 624 记录路径的0-1背包的相关文章

UVA 624(01背包记录路径)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=565 记录路径可以用一个二维数组,记录改变时的量.然后从后往前可以推得所有的值. #include <iostream> #include <string> #include <cstring> #include <cstdlib> #incl

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 记录路径DP

开一个数组p 若dp[i-1][j]<dp[i-1][j-a[i]]+a[i]时就记录下p[j]=a[i];表示此时放进一个轨道 递归输出p #include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #in

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

浙大PAT CCCC L3-001 凑零钱 ( 0/1背包 &amp;&amp; 路径记录 )

题目链接 分析 : 就是一个 0/1 背包,但是需要记录具体状态的转移情况 这个可以想象成一个状态转移图,然后实际就是记录路径 将状态看成点然后转移看成边,最后输出字典序最小的路径 这里有一个很巧妙的做法 先将所有的硬币升序排序(这一点很重要) 然后在这一条件下,假设当前状态是考虑第 i 个硬币,前一个状态是考虑第 i-1 个硬币 试想对于同一个体积,如果选用的硬币数量越多是不是字典序更小 然后对于如果对于同一体积下,选用硬币数一样多的两种方案 由于我们已经升序排序,如果有一样多硬币的情况,那么

POJ 1293 Duty Free Shop(背包记录路径)

Description Pedro travelled to Europe to take part in the International Olympiad in Informatics and is coming back home. Since all his friends asked him to bring them some gift, he bought two big bags of chocolates (one of Mindt and one of Lilka). Ea

poj1787Charlie&#39;s Change(多重背包+记录路径+好题)

Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3720   Accepted: 1125 Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motore

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 11374 最短路+记录路径 dijkstra最短路模板

UVA - 11374 Airport Express Time Limit:1000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu [Submit]  [Go Back]  [Status] Description ProblemD: Airport Express In a small city called Iokh, a train service, Airport-Express, takes residents to t