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>
#include <algorithm>

using namespace std;

const int maxn = 2005;

int a[22];
int dp[maxn];

int main()
{
    int len, n;
    while(~scanf("%d%d", &len, &n))
    {
        memset(dp, 0, sizeof(dp));
        for(int i=1; i<=n; i++)
        {
            scanf("%d", &a[i]);
        }
        dp[0] = 1;
        int Max = 0;
        for(int i=n; i>=1; i--)
        {
            for(int j=len; j>=a[i]; j--)
            {
                if(dp[j-a[i]]&&dp[j]==0)
                {
                    dp[j] = a[i];
                }
            }
        }
        while(dp[len]==0)
            len--;
        Max = len;
        while(len)
        {
            printf("%d ", dp[len]);
            len -= dp[len];
        }
        printf("sum:%d\n", Max);
    }

    return 0;
}
时间: 2025-01-15 14:49:11

UVA 624 CD (01背包 带路径)的相关文章

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

Uva 642-CD(0-1背包+打印路径)

题目链接:点击打开链接 裸01背包 ,此题中 价值即体积... 打印路径..不多说了 一维的没看懂..上个二维的 #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <string> #include <cctype> #include <vector> #include <cstdio>

UVA 624 (0 1背包 + 打印路径)

#include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #include<algorithm> #define N 1010 using namespace std; int dp[N], path[N][N], w[N]; int main() { int v, n; while(~scanf("%d", &v)) { sca

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

CD Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 62464-bit integer IO format: %lld      Java class name: Main You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music

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