#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) {// 不用…
/* 01背包第k优解问题 f[i][j][k] 前i个物品体积为j的第k优解 对于每次的ij状态 记下之前的两种状态 i-1 j-w[i] (选i) i-1 j (不选i) 分别k个 然后归并排序并且去重生成ij状态的前k优解 */ #include<iostream> #include<cstdio> #include<cstring> #define maxn 1010 using namespace std; ],x[maxn],y[maxn],a,b,z; i…
题意:给你一个体积为\(T\)的背包,有\(n\)个物品,每个物品的价值和体积都是是\(a_{i}\),求放哪几个物品使得总价值最大,输出它们,并且输出价值的最大值. 题解:其实就是一个01背包输出路径的裸题,直接上板子就行了.(一维的背包写法其实还是不太怎么怎么理解,具体的以后再补). 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include…
题目大意:有n件珠宝,每个珠宝的魅力值为v,重量为w,求在重量不超过m的情况下能达到的最大魅力值. 题目思路:简单的01背包,由于二维数组会超内存所以应该压缩成一维数组. dp[i][j],表示选取i件物品,重量为j的情况下能达到的最大魅力值,则容易推出dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+v[j]),可以看出当前的dp[i][j]只和前一轮有关,所以可以采用滚动数组的办法进行压缩:dp[j]=max(dp[j],dp[j-w[i]]+v[i]). #i…
题意:有n件手镯,总重量不能超过M,每个手镯有一个体重W[i]和魅力V[i],问在不超过M的情况下能获得的魅力总和 思路:把M当背包总容量,用0-1背包写 代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <stack> #include <queue> #include <math.h> #…
L3-001. 凑零钱 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 韩梅梅喜欢满宇宙到处逛街.现在她逛到了一家火星店里,发现这家店有个特别的规矩:你可以用任何星球的硬币付钱,但是绝不找零,当然也不能欠债.韩梅梅手边有104枚来自各个星球的硬币,需要请你帮她盘算一下,是否可能精确凑出要付的款额. 输入格式: 输入第一行给出两个正整数:N(<=104)是硬币的总个数,M(<=102)是韩梅梅要付的款额.第二行给出N枚硬币的…
http://www.51nod.com/onlineJudge/questionCode.html#problemId=1007&noticeId=15020 求出n个数的和sum,然后用sum/2作为背包容量,让n个数去放,求出一个最大价值,那么这就是其中一组的和,另外一组的和就是sum-dp[sum/2]; 注意这里的体积和价值都是a[i]; #include <iostream> #include <cstdio> #include <cmath> #i…
描述 过年的时候,大人们最喜欢的活动,就是打牌了.xiaomengxian不会打牌,只好坐在一边看着. 这天,正当一群人打牌打得起劲的时候,突然有人喊道:“这副牌少了几张!”众人一数,果然是少了.于是这副牌的主人得意地说:“这是一幅特制的牌,我知道整副牌每一张的重量.只要我们称一下剩下的牌的总重量,就能知道少了哪些牌了.”大家都觉得这个办法不错,于是称出剩下的牌的总重量,开始计算少了哪些牌.由于数据量比较大,过了不久,大家都算得头晕了. 这时,xiaomengxian大声说:“你们看我的吧!”于…
解题思路:对于01背包的状态转移方程式f[v]=max(f[v],f[v-c[i]+w[i]]);其实01背包记录了每一个装法的背包值,但是在01背包中我们通常求的是最优解, 即为取的是f[v],f[v-c[i]]+w[i]中的最大值,但是现在要求第k大的值,我们就分别用两个数组保留f[v]的前k个值,f[v-c[i]]+w[i]的前k个值,再将这两个数组合并,取第k名. 即f的数组会增加一维. http://blog.csdn.net/lulipeng_cpp/article/details/…
Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4178    Accepted Submission(s): 2174 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took pa…
题意: 数据是常规的01背包,但是求的不是最大容量限制下的最佳解,而是第k佳解. 思路: 有两种解法: 1)网上普遍用的O(V*K*N). 2)先用常规01背包的方法求出背包容量限制下能装的最大价值m,再以m为背包容量再进行一次01背包,dp[j]表示当物品的组合价值为j时,它们的体积之和的最小量.那么就求出了所有可能的价值,从1-m都有,但是其中一些是求不出来的,也就是骨头的价值不能组合成这个数字,那么就得过滤掉. #include <iostream> #include <cstdi…
先说说普通01包的状态问题吧 普通的01背包,在状态转移的过程中为了求出最优解,一定是遍历了所有的情况 然后再求的最优解.那么对于第k最优解问题,我们只需要再加一个维度,用来记录每一个状态k优解的状态就好了. 在普通背包过程中 每次的选举的状态为dp[i-1][j],dp[i-1][j-c[i]+w[i]  为了求解最优情况 我们一般是对这两个状态取最大值 然后依次遍历 得到最大值.那么,为了得到第k大的解,我们就需要另外使用数组来对两种状态的所有值记录下来 然后选取第k大的情况 上代码 #in…
题目链接:https://www.rqnoj.cn/problem/57 题意: sqybi在七夕这天太无聊了,所以他想去给自己找GF. 总共有n个MM. 搞定第i个MM要花费rmb[i]块大洋.rp[i]的人品.tim[i]的时间. 现在sqybi有m块大洋.r个人品. 问你在泡到MM的数量最多的情况下,所用的最短时间是多少. 题解: 表示状态: dp[i][j][k][p] = min cost time i:考虑到第i个MM j:花费的rmb k:花费的rp p:已经搞定的MM数量 找出答…
Bone Collector http://acm.hdu.edu.cn/showproblem.php?pid=2602 Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to…
Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 48618    Accepted Submission(s): 20269 Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bo…
#include<stdio.h> #include<string.h> #define N 1100 int dp[N]; int main() { int n,t,m,a[N],b[N],i,j,v; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&v); for(i=1;i<=n;i++) scanf("%d",&a[i]); for(…
http://acm.hdu.edu.cn/showproblem.php?pid=2639       Problem Description The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup" competition,you must have seem this title.If you haven't seen it before,it does…
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…
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; ][],val[],vol[], A[],B[]; int main() { int T,n,v,K,k; scanf("%d",&T); while(T--) { scanf("%d%d%…
  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…
The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup" competition,you must have seem this title.If you haven't seen it before,it doesn't matter,I will give you a link: Here is the link: http://acm.hdu.edu.c…
In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5472    Accepted Submission(s): 1843 Problem Description Since 1945, when the first nuclear bomb was exploded by the Manhattan Project t…
描述 上午的训练结束了,THU ACM小组集体去吃午餐,他们一行N人来到了著名的十食堂.这里有两个打饭的窗口,每个窗口同一时刻只能给一个人打饭.由于每个人的口味(以及胃口)不同,所以他们要吃的菜各有不同,打饭所要花费的时间是因人而异的.另外每个人吃饭的速度也不尽相同,所以吃饭花费的时间也是可能有所不同的. THU ACM小组的吃饭计划是这样的:先把所有的人分成两队,并安排好每队中各人的排列顺序,然后一号队伍到一号窗口去排队打饭,二号队伍到二号窗口去排队打饭.每个人打完饭后立刻开始吃,所有人都吃完…
文章目录 背包问题中的常用变量说明 题目 解题思路 我想要想理解最简单 01背包就是要`理解…
题目链接: 题目 In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) 问题描述 Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the number of nuclear weapons have soared across th…
http://acm.hdu.edu.cn/showproblem.php?pid=2639 01背包第k优解,把每次的max分步列出来即可 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct Node { int price; int val; }node[]; int main() { int T; scanf("%d",&…
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…
# include <stdio.h> # include <stdlib.h> # include <string.h> # define max(x,y) x>y?x:y; ];//价值 ];//重量 ][]; int main() { int n,m; while(scanf("%d%d",&m,&n)!=EOF) { memset(dp,,sizeof(dp));//初始化 ; i<=n; i++) scanf(&…
来自http://www.geeksforgeeks.org/dynamic-programming-set-7-coin-change/ 对于整数N,找出N的所有零钱的表示.零钱可以用S={s1,s2,s3,..sm}表示,每种零钱的数量为无穷.请问有多少种找零的方法? 例如, N = 4,S = {1,2,3},有四种找零方式{1,1,1,1},{1,1,2},{2,2},{1,3},return 4 N = 10,S= {2,5,3,6} ,有5中找零方式{2,2,2,2,2}, {2,2…
L3-001. 凑零钱 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 韩梅梅喜欢满宇宙到处逛街.现在她逛到了一家火星店里,发现这家店有个特别的规矩:你可以用任何星球的硬币付钱,但是绝不找零,当然也不能欠债.韩梅梅手边有104枚来自各个星球的硬币,需要请你帮她盘算一下,是否可能精确凑出要付的款额. 输入格式: 输入第一行给出两个正整数:N(<=104)是硬币的总个数,M(<=102)是韩梅梅要付的款额.第二行给出N枚硬币的…