【hdu1059】多重背包部分和
来源:互联网 发布:算法 中文版 pdf 编辑:程序博客网 时间:2024/05/18 02:09
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 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 describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, …, 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.
Output
For each colletcion, 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.
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~6的弹珠,不同价值的弹珠有不同的数量。两个人要分成相同的价值,问能不能分成功。
思路:
dfs有2^120000种选择100%TLE。可转化成多重背包问题,即背包内是否能恰好装满sum/2,利用二进制优化背包,sum&1=1可剪枝。
dp[j]代表不超过j价值的最大价值,所以判断dp[sum/2]==sum/2
#include<bits/stdc++.h>using namespace std;const int N =210005;int dp[N],a[12005],c[8];int main(){ int i,j,cnt,co=0; while(1) { int sum=0; cnt=0; for(i=1; i<=6; i++) { scanf("%d",&c[i]); sum+=c[i]*i; } if(sum==0) break; printf("Collection #%d:\n",++co); if(sum&1) { puts("Can't be divided.\n"); continue; } sum/=2; for(i=1; i<=6; i++) { for(j=1; j<=c[i]; j<<=1) { a[++cnt]=j*i; c[i]-=j; } if(c[i]!=0) a[++cnt]=i*c[i]; } memset(dp,0,sizeof(dp)); for(i=1; i<=cnt; i++) for(j=sum; j>=a[i]; j--) dp[j]=max(dp[j],dp[j-a[i]]+a[i]); if(dp[sum]!=sum) puts("Can't be divided.\n"); else puts("Can be divided.\n"); } return 0;}
- 【hdu1059】多重背包部分和
- HDU1059 多重背包 多重部分和问题DP
- hdu1059 Dividing_多重部分和问题
- hdu1059(多重背包)
- hdu1059(多重背包)
- hdu1059 Dividing 多重背包
- hdu1059 多重背包
- [hdu1059]Dividing 多重背包
- hdu1059 多重背包
- hdu1059 Dividing (多重背包)
- hdu1059(多重背包)
- hdu1059 多重背包
- hdu1059 多重背包
- hdu1059 Dividing 【多重背包】
- HDU1059 && POJ1014 :Dividing(多重背包)
- hdu1059 &poj1014 Dividing (多重背包)
- HDU1059 Dividing 多重背包 ACM
- poj1014 hdu1059 Dividing 多重背包
- iPhoneX的启动图如何配置
- 驼峰命名法与数据库下划线命名的字符串快速转化,方便操作数据
- synchronizing script resources with Tern server
- loj117 有源汇有上下界最小流
- laravel 验证码
- 【hdu1059】多重背包部分和
- 属性动画
- 单链表
- 仿京东购物车
- 【BZOJ1096】球形空间产生器
- 解决AndroidStudio导入项目在 Building gradle project info 一直卡住
- 使用“哨兵”减小时间复杂度
- [UnityEditor基础]脚本自动定位选择Hierarchy或Project下的对象
- web返回固定页面