UVA 624 - CD (01背包 + 打印物品)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=565
题意很好理解,普通的01背包,dp[i - 1][j]表示在前i - 1件物品中选取若干物品放入容量为j背包所得到的最大的价值,dp[i - 1][j - w[i]] + v[i]表示前i - 1件物品中选取若干物品放入容量为j - w[i]背包所得到的最大的价值加上第i件物品的价值。这里同理,但是要记录选取的物品,觉得倒着过来就可以了。
- #include <iostream>
- #include <cstring>
- #include <vector>
- using namespace std;
- const int MAXN = ;
- int dp[][MAXN] , v[];
- vector <int> G;
- int main()
- {
- ios::sync_with_stdio(false);
- int V , n;
- while(cin >> V >> n) {
- G.clear();
- for(int i = ; i <= n ; i++) {
- cin >> v[i];
- }
- memset(dp , , sizeof(dp));
- for(int i = ; i <= n ; i++) {
- for(int j = ; j <= V ; j++) {
- if(j < v[i])
- dp[i][j] = dp[i - ][j];
- else
- dp[i][j] = max(dp[i - ][j] , dp[i - ][j - v[i]] + v[i]);
- }
- }
- int i = n , j = V;
- while(i > && j > ) {
- if(j < v[i]) {
- i--;
- continue;
- }
- if(dp[i][j] == dp[i - ][j - v[i]] + v[i]) {
- j -= v[i];
- G.push_back(v[i]);
- }
- i--;
- }
- for(int i = G.size() - ; i > ; i--) {
- cout << G[i] << " ";
- }
- cout << G[] << " sum:";
- cout << dp[n][V] << endl;
- }
- }
UVA 624 - CD (01背包 + 打印物品)的更多相关文章
- uva 624 CD 01背包打印路径
// 集训最终開始了.来到水题先 #include <cstdio> #include <cstring> #include <algorithm> #includ ...
- UVA 624 ---CD 01背包路径输出
DescriptionCD You have a long drive by car ahead. You have a tape recorder, but unfortunately your b ...
- UVA 624 CD (01背包)
//路径记录方法:若是dp[j-value[i]]+value[i]>dp[j]说明拿了这个东西,标志为1, //for循环标志,发现是1,就打印出来,并把背包的容量减少,再在次容量中寻找标志: ...
- UVA624 CD,01背包+打印路径,好题!
624 - CD 题意:一段n分钟的路程,磁带里有m首歌,每首歌有一个时间,求最多能听多少分钟的歌,并求出是拿几首歌. 思路:如果是求时常,直接用01背包即可,但设计到打印路径这里就用一个二维数组标记 ...
- UVA 624 (0 1背包 + 打印路径)
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #i ...
- UVA 624 CD(01背包+输出方案)
01背包,由于要输出方案,所以还要在dp的同时,保存一下路径. #include <iostream> #include <stdio.h> #include <stri ...
- UVA 624 CD(DP + 01背包)
CD You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music i ...
- uva 624 CD (01背包)
CD You have a long drive by car ahead. You have a tape recorder, but unfortunately your best musi ...
- CD(01背包)
You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is o ...
随机推荐
- IIS 10.0 无法安装 URL rewrite重写模块 2.0解决办法
[问题描述]系统升级到Windows10后,IIS是10.0的,发现无法安装 URLRewrite重写模块 2.0. [解决办法]打开注册表编辑器,在HKEY_LOCAL_MACHINE\SOFTWA ...
- 手动配置gradle
最近从github倒入项目,运行的特别慢gradle配置有问题,解决方法: 1.C:\android\demo\hellocharts-android-master\gradle\wrapper 目录 ...
- crtbegin_dynamic.o: No such file: No such file or directory
/homesec/android2/zhangbin/053work3/hi050src/HiSTBAndroidV400R001C00SPC050B012/prebuilt/linux-x86/to ...
- 使php支持mbstring库
多国语言并存就意味着多字节,PHP内置的字符串长度函数strlen无法正确处理中文字符串,它得到的只是字符串所占的字节数.对于GB2312的中文编码,strlen得到的值是汉字个数的2倍,而对于UTF ...
- zoj 2770 Burn the Linked Camp (差分约束系统)
// 差分约束系统// 火烧连营 // n个点 m条边 每天边约束i到j这些军营的人数 n个兵营都有容量// Si表示前i个军营的总数 那么 1.Si-S(i-1)<=C[i] 这里 建边(i- ...
- mkswap 把一个分区格式化成为swap交换区
mkswap /dev/sda* //创建此分区为swap 交换分区swapon /dev/sda* //加载交换分区swapoff /dev/sda* //关闭交换分区: 若想要开机自动挂载:vi ...
- 【MySQL for Mac】终极解决——MySQL在Mac的字符集设置
这个问题烦恼一天了,现在终于得以解决.分享给大家 首先贴出来,亲测不可行的博客连接: http://www.2cto.com/database/201305/215563.html http://bl ...
- MyBatis批量删除 多态sql,构建in语句
<!--==========================删除==================================== --> <delete id=&quo ...
- Delphi 利用TComm组件 Spcomm 实现串行通信
Delphi 利用TComm组件 Spcomm 实现串行通信 摘要:利用Delphi开发工业控制系统软件成为越来越多的开发人员的选择,而串口通信是这个过程中必须解决的问题之一.本文在对几种常用串口通信 ...
- delphi7在win7系统如何安装spcomm控件
1.先准备好串口控件SPCOMM,例如把它放在F盘的工具安装文件夹下,等一下加载时需要用到. 2.打开delphi7软件. 3.按下上面的Component>Install Component ...