背包问题模板,POJ(1014)】的更多相关文章

这个问题是存在做.我发现即使是可行的一个问题,但不一定正确. 大部分数据疲软,因为主题. id=1014">poj 1014 Dividing 题目大意:有6堆石头,权重分别为1 2 3 4 5 6,要求输入 每堆个数 ,求能否够平分石头使得两堆价值同样. 网上对这道题的做法就两种,当中有错误的版本号.却也能够AC. 起初这让我等菜鸟感慨代码的简洁,但无法得出正确性的证明 接下来就对两种方法的错误性进行证明. 1.多重背包 #include <map> #include<…
背包问题模板 一.0-1背包 状态:背包容量为j时,求前i个物品所能达到最大价值,设为dp[i][j].初始时,dp[0][j](0<=j<=V)为0,没有物品也就没有价值. 状态转移方程:由上述分析,第i个物品的体积为w,价值为v,则状态转移方程为 j<w,dp[i][j] = dp[i-1][j] //背包装不下该物品,最大价值不变 j>=w, dp[i][j] = max{ dp[i-1][j-list[i].w] + v, dp[i-1][j] } //和不放入该物品时同样…
题目链接:http://poj.org/problem?id=1014 背包问题太经典了,之前的一篇博客已经讲了背包问题的原理. 这一个题目是多重背包,但是之前的枚举是超时的,这里采用二进制优化. 这是所有01背包,完全背包,多重背包的模板哦! #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int sum;…
#include <iostream> #include <stdio.h> #include <cstring> #define INF 100000000 using namespace std; ]; //f[j]相当于f[i][j]: 考虑1...i个物品,恰好放到容量为j,所能达到的最大价值 int v; //背包容量 void complete_pack(int *dp, int c, int w) { for(int i = c; i <= v; i…
原题目:http://poj.org/problem?id=1014 题目大意: 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两份的总价值相等,其中一个物品不能切开,只能分给其中的某一方,当输入六个0是(即没有物品了),这程序结束,总物品的总个数不超过20000 输出:每个测试用例占三行: 第一行: Collection #k: k为第几组测试用例 第二行:是否能分(具体形式见用例) 第三行:空白(必须注意,否则PE) 一:…
http://poj.org/problem?id=1014 题意:6个物品,每个物品都有其价值和数量,判断是否能价值平分. 思路: 多重背包.利用二进制来转化成0-1背包求解. #include<iostream> #include<string> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; ; int sum; ]; int d[max…
  Time Limit: 4000MS   Memory Limit: 65536K Total Submissions:32863   Accepted: 8953 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he told them a st…
Dividing   Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they cou…
多重背包模板- #include <stdio.h> #include <string.h> int a[7]; int f[100005]; int v, k; void ZeroOnePack(int cost, int weight) { for (int i = v; i >= cost; i--) if (f[i - cost] + weight > f[i]) f[i] = f[i - cost] + weight; } void CompletePack(…
Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15944   Accepted: 8167 Description On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertical…
A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59798   Accepted: 18237 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of…
Q: 倍增优化后, 还是有重复的元素, 怎么办 A: 假定重复的元素比较少, 不用考虑 Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the…
Problem Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could…
资料1:http://blog.csdn.net/regina8023/article/details/41910615 资料2:模板来源:http://www.cnblogs.com/lidaxin/category/794693.html 主席树主要是维护区间在第i个数插入之后,每个区间内的数出现的次数,这样就需要维护n颗线段树. 可持久化在于它可以保存前面任意第i个数插入时的区间情况 同时每颗树结构都是一样的,因此可以相减得到特定区间的情况 #include <queue> #inclu…
二进制优化,事实上是物体的分解问题. 就是比方一个物体有数量限制,比方是13,那么就须要把这个物体分解为1. 2, 4, 6 假设这个物体有数量为25,那么就分解为1, 2, 4. 8. 10 看出规律吗,就是分成2的倍数加上位数,比方6 = 13 - 1 - 2 - 4, 10 = 25 - 1 - 2 - 4 - 8.呵呵,为什么这么分解? 由于这样分解之后就能够组合成全部1到13的数.为25的时候能够组合成全部1到25的数啦. 就是这么一个分解物体.最后组合的问题. 不明确? 给多几个数字…
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: Accepted: Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if…
//截取字符串 ch 的 st~en 这一段子串返回子串的首地址 //注意用完需要根据需要最后free()掉 char* substring(char* ch,int st,int en) { ; char* pch=ch; ); pch=pch+st; ;i<length;i++) subch[i]=*(pch++); subch[length]='\0'; return subch; } 字符串截取 POJ 3080 Blue Jeans 题意 : 给出 n 个包含 60 个字符的字符串,问…
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 66032 Accepted: 17182 Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles.…
Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 69575   Accepted: 18138 Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbl…
题目:Dividing 题意:6种重量的的石头,每个给定数量,用总重的一半去装,问能否装满. #include <iostream> #include <algorithm> #include <stdlib.h> #include <time.h> #include <cmath> #include <cstdio> #include <string> #include <cstring> #include…
Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63980   Accepted: 16591 Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbl…
Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 32955   Accepted: 11199 Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some…
#include <iostream> #include <cstdio> #include <memory.h> using namespace std; int n,m,num,temp,sum; ][],link[];//牛与牛栏的对应关系 ];//增益路径 bool DFS(int a) { ;i<=m;i++) { && !tag[i])//如果节点i与a相邻并且未被查找过 { tag[i]=true;//标记i为已查找过 ||DFS(l…
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #define MAX 102 void read(); using namespace std; int map[MAX][MAX],best[MAX],n; bool visit[MAX]; int main() { //freopen("in.txt","r",stdin…
题意:给出价值为1,2,3,4,5,6的6种物品数量,问是否能将物品分成两份,使两份的总价值相等. 思路:求出总价值除二,做多重背包,需要二进制优化. 代码: #include<iostream> #include<cstdio> #include<cstring> using namespace std; int n[7]; int v,sum; bool flag; int dp[100000]; /*完全背包*/ void CompletePack(int cos…
多重背包模型  写的时候漏了一个等号找了半天 i<<=1 !!!!!! #include<iostream> #include<cstdio> #include<cstring> using namespace std; ]; ]; ]; int main(){ ; ; ])==){ ]; ; ;j<=a[];j<<){ m[flag++]=j; a[]-=j; } ])m[flag++]=a[]; ;i<n;i++){ scanf(…
题意 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两份的总价值相等,其中一个物品不能切开,只能分给其中的某一方,当输入六个0是(即没有物品了),这程序结束,总物品的总个数不超过20000 思路 裸的多重可行性背包,设dp[i]表示容量为i是否可装.状态设计看代码吧. 代码 [cpp] #include <iostream> #include <cstdio> #include <cmath> #in…
#include<iostream>#include<stdio.h>#define num 6using namespace std; bool DFS(int i,int half_sum);int a[num];int half_value;int main(){    //freopen("acm.acm","r",stdin);    int i;    int sum;    int time = 0;    while(1)  …
Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16941    Accepted Submission(s): 5190 Problem Description Give you a sequence and ask you the kth big number of a inteval.   Input The…
几个比较好的博客 http://www.renfei.org/blog/isap.html http://kenby.iteye.com/blog/945454 http://blog.csdn.net/mypsq/article/details/37959249 #include<stdio.h> #include<string.h> #include<queue> using namespace std; #define inf 999999 #define N 3…