// 集训最终開始了。来到水题先

#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 >> a[i];
memset(flag,0,sizeof(flag));
memset(d,0,sizeof(d));
} void print(int x,int y){
if (y<=0||x<1) return ;
//cout << x << " " << y << endl;
if (d[x][y]==d[x-1][y]){
flag[x] = 0;
print(x-1,y);
//return ;
}else if (d[x][y]==d[x-1][y-a[x]] + a[x]){
flag[x] = 1;
//cout << a[x] << " ";
print(x-1,y-a[x]);
//return ;
}
} void solve(){
for (int i=1;i<=n;i++){
for (int j=W;j>=0;j--){
d[i][j] = d[i-1][j];
if (j>=a[i])
d[i][j] = max(d[i-1][j],d[i-1][j-a[i]] + a[i]);
}
} print(n,W); for (int i=1;i<=n;i++)
if (flag[i])
printf("%d ",a[i]);
printf("sum:%d\n",d[n][W]);
} int main(){
//freopen("1.txt","r",stdin);
while(cin >> W){
init();
solve();
}
}

uva 624 CD 01背包打印路径的更多相关文章

  1. UVA 624 - CD (01背包 + 打印物品)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  2. UVA 624 ---CD 01背包路径输出

    DescriptionCD You have a long drive by car ahead. You have a tape recorder, but unfortunately your b ...

  3. UVA624 CD,01背包+打印路径,好题!

    624 - CD 题意:一段n分钟的路程,磁带里有m首歌,每首歌有一个时间,求最多能听多少分钟的歌,并求出是拿几首歌. 思路:如果是求时常,直接用01背包即可,但设计到打印路径这里就用一个二维数组标记 ...

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

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #i ...

  5. UVA 624 CD (01背包)

    //路径记录方法:若是dp[j-value[i]]+value[i]>dp[j]说明拿了这个东西,标志为1, //for循环标志,发现是1,就打印出来,并把背包的容量减少,再在次容量中寻找标志: ...

  6. (动态规划 01背包 打印路径) CD --UVA --624

    链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87813#problem/G 每个CD的时间不超过 20没有哪个CD的时间是超过N ...

  7. UVA624(01背包记录路径)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  8. Codeforces Gym-102219 2019 ICPC Malaysia National E. Optimal Slots(01背包+输出路径)

    题意:给你一个体积为\(T\)的背包,有\(n\)个物品,每个物品的价值和体积都是是\(a_{i}\),求放哪几个物品使得总价值最大,输出它们,并且输出价值的最大值. 题解:其实就是一个01背包输出路 ...

  9. UVA 624 CD(01背包+输出方案)

    01背包,由于要输出方案,所以还要在dp的同时,保存一下路径. #include <iostream> #include <stdio.h> #include <stri ...

随机推荐

  1. git 日常使用

    git clone git checkout      git 删除 本地分支: git branch -d <本地分支名> git branch -D <本地分支名>(大写表 ...

  2. 函数flst_get_last

    flst_node_t中存有12个字节的内容,前6个字节(page:4 boffset:2)表示相对自己前一个node的fil_addr_t信息,后6个字节表示相对自己后1个node的fil_addr ...

  3. mvc 相关js

    http://modernizr.com/ https://github.com/Modernizr/Modernizr/wiki 主要看下Polyfills 用于html5,用于一些老ie,fire ...

  4. 【转】android开发工具Eclipse,androidStudio,adt网盘下载--不错

    原文网址:http://tools.android-studio.org/index.php/85-tools/109-android-tools-download

  5. 基于MongoDB分布式存储进行MapReduce并行查询

    中介绍了如何基于Mongodb进行关系型数据的分布式存储,有了存储就会牵扯到查询.虽然用普通的方式也可以进行查询,但今天要介绍的是如何使用MONGODB中提供的MapReduce功能进行查询.     ...

  6. Progress 自定义(一)-shape

    需求:自定义ProgressBar,使用系统自定义shape; 效果图: 1.默认底色: 2.第一进度颜色: 3.第二进度颜色: 实现分析: 1.目录结构: 代码实现: 1.progress_styl ...

  7. (转载)memcpy的几个实现版本

    (转载)http://blog.sina.com.cn/s/blog_4d3a41f40100cvza.html 实现void *memcpy(void *to, const void *from, ...

  8. LoadRunner常见问题整理(转)

    首先要感谢群友的无私分享,才能得到这篇好的学习资料,整理得太好了,所以收藏保存,方便以后学习. 一:LoadRunner常见问题整理 1.LR 脚本为空的解决方法: 1.去掉ie设置中的第三方支持取消 ...

  9. REST API TESTING

    在敏捷开发过程中 每隔两周就是一个sprint,,, 在上个sprint中,任务就是REST API TESTING 因为以前没做过API 测试,不懂,然后经过询问查找 终于知道,需要发送请求,然后获 ...

  10. 解决adb问题的方法

    The connection to adb is down,and a server error has occured. 在网上找的那个高端方法根本不管用,来,试试我的方法.. 先装个360手机助手 ...