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(…
题目http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=565 分析:题目是一个01背包问题.但是增加了路径输出. 由于路径,所以才有二维递推的形式. dp[i,j]=max{ dp[i-1,j], dp[i-1,j-m[i]]+m[i]} ​在输出集合的时候,如果dp[i,j]==dp[i-1,j],那么表明第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…
题意:给你一个体积为\(T\)的背包,有\(n\)个物品,每个物品的价值和体积都是是\(a_{i}\),求放哪几个物品使得总价值最大,输出它们,并且输出价值的最大值. 题解:其实就是一个01背包输出路径的裸题,直接上板子就行了.(一维的背包写法其实还是不太怎么怎么理解,具体的以后再补). 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include…
描述 过年的时候,大人们最喜欢的活动,就是打牌了.xiaomengxian不会打牌,只好坐在一边看着. 这天,正当一群人打牌打得起劲的时候,突然有人喊道:“这副牌少了几张!”众人一数,果然是少了.于是这副牌的主人得意地说:“这是一幅特制的牌,我知道整副牌每一张的重量.只要我们称一下剩下的牌的总重量,就能知道少了哪些牌了.”大家都觉得这个办法不错,于是称出剩下的牌的总重量,开始计算少了哪些牌.由于数据量比较大,过了不久,大家都算得头晕了. 这时,xiaomengxian大声说:“你们看我的吧!”于…
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…
题目链接: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件物…
link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=565 用一个二维数组g[i][v]表示:当状态转移到v的时候,第i个物品是不是用到,如果用到标记1,否则标记0. 输出路径的时候,注意,从物品编号0一直到n-1.如果某个物品被用到了,g[i][v]里面的v,就要减去这个物品的体积,然后继续往下找. /* * ============…
#include <iostream> #include <algorithm> #include <vector> #include <cstdio> #include <cstring> using namespace std; ; int a[N]; ]; vector < vector <); int n,m; bool cmp (vector<int> x, vector<int> y) {// 不用…
You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is onCDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. Howto choose tracks from CD to get most out of tape spa…