POJ 1014 Dividing(二进制优化+多重背包)
来源:互联网 发布:邦奇-威尔斯 知乎 编辑:程序博客网 时间:2024/05/15 00:23
Dividing
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 63646 Accepted: 16487
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 just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value. Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
Input
Each line in the input file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , n6 , where ni is the number of marbles of value i. So, the example from above would be described by the input-line "1 0 1 2 0 0". The maximum total number of marbles will be 20000.
The last line of the input file will be "0 0 0 0 0 0"; do not process this line.
The last line of the input file will be "0 0 0 0 0 0"; do not process this line.
Output
For each collection, output "Collection #k:", where k is the number of the test case, and then either "Can be divided." or "Can't be divided.".
Output a blank line after each test case.
Output a blank line after each test case.
Sample Input
1 0 1 2 0 0 1 0 0 0 1 1 0 0 0 0 0 0
Sample Output
Collection #1:Can't be divided.Collection #2:Can be divided.
题目大意:有权值分别为1,2,3,4,5,6的大理石,每种都有若干块,也可能没有,问你能否把它们分成权值相等的2份。大理石的总数量不超过20000。
普通的多重背包时间复杂度为O(V*Σn(i)) V为空间容量,n(i)为每种石头的数量限制。题目看上去是多重背包,背包容量为石子权值总和的一半。但是我们可以通过二进制优化将其变成01背包(详见背包九讲V2.0章节2.4)。所谓的二进制优化就是把一个正整数拆成2^n之和和剩余的数。比如17,我们先拆分出1,然后是2、4、8,这是我们发现如果再往后拆的话1+2+4+8+16=31>17,所以拆分不出16,就只能剩下一个17-1-2-4-8=2了。所以我们把17拆分成了1,2,4,8,2,从1到17的所有正整数都能由着这5个数相加而成。
于是我们把多重背包简化成了01背包,把多重背包每种物品取的数量转化成了若干个数的取与不取。最终我们把问题化成了01背包,时间复杂度下降很多。
#include<stack>#include<queue>#include<cmath>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#pragma commment(linker,"/STACK: 102400000 102400000")#define lson a,b,l,mid,cur<<1#define rson a,b,mid+1,r,cur<<1|1using namespace std;typedef long long LL;const double eps=1e-6;const int MAXN=131000;int dp[MAXN],value[MAXN],n[8],sum;int main(){#ifndef ONLINE_JUDGE freopen("in.txt","r",stdin);#endif // ONLINE_JUDGE int cnt=0; while(scanf("%d%d%d%d%d%d",&n[1],&n[2],&n[3],&n[4],&n[5],&n[6])!=EOF) { if(n[1]+n[2]+n[3]+n[4]+n[5]+n[6]==0) break; if(cnt) printf("\n"); sum=n[1]*1+n[2]*2+n[3]*3+n[4]*4+n[5]*5+n[6]*6; int half=sum/2; int c=1; memset(value,0,sizeof(value)); memset(dp,0,sizeof(dp)); for(int i=1;i<=6;i++) { for(int j=1;j<=n[i];j*=2)//二进制优化,每次拆分出一个2^j { value[c++]=j*i; n[i]-=j; } if(n[i]>0)//剩余的数单独计算 value[c++]=n[i]*i; } for(int i=1;i<=c-1;i++)//背包容量为石子权值总和的一半 for(int j=half;j>=value[i];j--) dp[j]=max(dp[j],dp[j-value[i]]+value[i]); if(dp[half]==sum-half) printf("Collection #%d:\nCan be divided.\n",++cnt); else printf("Collection #%d:\nCan't be divided.\n",++cnt); } return 0;}
2 0
- POJ 1014 Dividing(二进制优化+多重背包)
- POJ 1014 Dividing(多重背包 + 二进制优化 + 01背包)
- POJ 1014 Dividing 【DP 之 多重背包 / 二进制优化】
- poj 1014 Dividing 多重背包 二进制拆分
- Dividing(多重背包+二进制优化)
- 【POJ】 1014 Dividing(多重背包,优化)
- poj 1014 Dividing(组合数学方法优化/多重背包问题+二进制优化)
- POJ 1014 dividing(多重背包+二进制拆分)
- poj1014 dividing(多重背包+二进制优化)
- HDU Dividing (多重背包+二进制优化)
- HDU 1059 Dividing (多重背包+二进制优化)
- hdu 1059 Dividing 多重背包,二进制优化
- HDU 1059 Dividing 多重背包二进制优化
- hdu1059 Dividing(多重背包+二进制优化)
- hdu1059 Dividing(多重背包+二进制优化)
- #POJ1014#Dividing(多重背包二进制优化)
- HDU1059:Dividing(多重背包二进制优化)
- HDU1059-Dividing(多重背包+二进制优化)
- c语言字符数组与字符串的使用详解
- 几种java通信(rmi,http,hessian,webservice)协议性能比较
- php验证码--图片
- LNMP环境搭建——MySQL篇
- Linux下的内核测试工具——perf使用简介
- POJ 1014 Dividing(二进制优化+多重背包)
- LeetCode222:Count Complete Tree Nodes
- HDOJ1272~小希的迷宫~并查集
- 将博客搬至CSDN
- button的内边距
- iOS编程——Swift实现一个3D可旋转的立方体(可做菜单页)
- ZigZag Conversion
- BJFU 1398 蚂蚁下落
- Netty中execution包功能详解