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件物…
DescriptionCD 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…
//路径记录方法:若是dp[j-value[i]]+value[i]>dp[j]说明拿了这个东西,标志为1, //for循环标志,发现是1,就打印出来,并把背包的容量减少,再在次容量中寻找标志: #include <iostream> #include <cstring> #include <algorithm> using namespace std; ],dp[],s[][]; int main() { int n,m; while(cin>>m)…
// 集训最终開始了.来到水题先 #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 >…
01背包,由于要输出方案,所以还要在dp的同时,保存一下路径. #include <iostream> #include <stdio.h> #include <string.h> /* AC 01背包+输出方案 答案不唯一,就像样例中的 45 8 4 10 44 43 12 9 8 2 题目给出的输出4 10 12 9 8 2 sum:45 输出43 2 sum:45 也是可以的 题目中没要求按照什么顺序输出,输出一种方案即可 */ using namespace s…
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 out of tap…
  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 out of…
624 - CD 题意:一段n分钟的路程,磁带里有m首歌,每首歌有一个时间,求最多能听多少分钟的歌,并求出是拿几首歌. 思路:如果是求时常,直接用01背包即可,但设计到打印路径这里就用一个二维数组标记一下即可. const int N=1e3+10; int n,m,a[N],d[N],v[N][N]; int main() { while(~scanf("%d%d",&n,&m)) { memset(v,0,sizeof(v)); memset(d,0,sizeof(…
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 out of tape s…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2126 有n个物品每个物品的价格是v[i],现在有m元钱问最多买多少种物品,并求出有多少种选择方法: 如果将物品的价格看做容量,将它的件数1看做价值的话,那么用01背包就可以求的花费m钱所能买到的最大件数dp[m]. 但是题目还要求方案数,因此很容易想到再建立一个数组f[j],存储j元钱能买dp[j]个物品的方案数. 在求解01背包的过程中,要分两种情况讨论: 设当前所选的物品为i 1.   若选了物…