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>
#include <string>
#include<iostream>
#include <algorithm>
using namespace std;
#define MAXN 11111
#include <queue>
#include <vector>
#define IN freopen("in.txt","r",stdin);
int a[30],dp[30][MAXN],p[MAXN];
void out(int x)
{
    if(x==0)
        return ;
    else    out(x-p[x]);
    printf("%d ",p[x]);
}
int main()
{
    int n,m;
   // IN;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        memset(p,0,sizeof(p));
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        for(int i=1; i<=n; i++)
        {
            for(int j=m; j>0; j--)
            {
                if(j>=a[i]&&dp[i-1][j]<dp[i-1][j-a[i]]+a[i])
                    dp[i][j]=dp[i-1][j-a[i]]+a[i],p[j]=a[i];
                else
                    dp[i][j]=dp[i-1][j];
            }
        }
        int i;
        for(i=m;i>=0;i--)
            if(dp[n][i]>dp[n][i-1])
                break;
        out(i);
        printf("sum:%d\n",dp[n][m]);
    }
    return 0;
}

UVA 624 CD 记录路径DP

时间: 2024-08-10 02:24:07

UVA 624 CD 记录路径DP的相关文章

【DP】UVA 624 CD 记录路径

开一个数组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背包)

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背包+打印路径 或 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背包 带路径)

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

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

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

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

URAL 1244 Gentlement DP +记录路径 好题

1244. Gentlemen Time limit: 0.5 secondMemory limit: 64 MB Let's remember one old joke: Once a gentleman said to another gentleman:— What if we play cards?— You know, I haven't played cards for ten years…— And I haven't played for fifteen years…So, li