RePorridge

Nothing change but our heart

HDU 1059 Dividing DP 背包 + 二进制优化

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的玻璃球的数量,然后让你判断能否将它们平分成价值相等的两份

思路:刚开始想到的是普通的0-1背包,遍历每个玻璃球然后刷新状态就可以的。结果交上去TLE了

然后网上找到了半天说是要用二进制优化。然后让我来解释一下我理解的二进制优化吧

用mar[i]表示价值为i的玻璃球的总数量。二进制优化就是把你的等价值的玻璃球分成系数为1,2,4,8,...,2^(k-1),mar[i]-2^k+1.其中k 为满足 mar[i]-2^k+1 > 0 的最大值.

这样就可以分别组合成1,2,3,4,5,6,7,...,mar[i]了。玻璃球就被分为1*i,2*i,4*i,8*i,...,2^(k-1)*i,(mar[i]-2^k+1)*i

然后就可以根据0-1背包的原理做了。这样就不会超时了

Dividing

posted on 2013-11-01 12:44 Porridge 阅读(340) 评论(0)  编辑 收藏 引用 所属分类: HDU题解


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

统计

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜