1. Time Limit: 1000MS Memory Limit: 10000K
  2. 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 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.

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.

Sample Input

  1.  

Sample Output

  1. Collection #:
  2. Can't be divided.
  3.  
  4. Collection #:
  5. Can be divided.

解题思路

因为是第一道例题,所以只做一些大佬AC代码的笔记,以下是参考博客的讲解:

如果总价值为奇数,那么肯定是不能分的。如果总价值为偶数,也不一定能分,因为一个弹珠是不能被拆分的。

以总价值的1/2为背包容量,进行动态规划求解。还用了二进制优化的方法,可以说这道题目是简单的多重背包吧。

dp[x]=1表示这些弹珠可以凑出价值为x的部分,否则就是不能凑成价值为x的部分。

参考博客

https://blog.csdn.net/u011561033/article/details/39526897

 AC代码

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. int main()
  6. {
  7. int n,sum,i,j,k,cs=;
  8. int a[];
  9. int dp[]; //大于6*20000
  10. while(scanf("%d",&a[])!=-)
  11. {
  12. sum=a[];
  13. for(i=; i<=; i++)
  14. {
  15. scanf("%d",&a[i]);//i为1,2,3,4,5,6,存储各类弹珠数量
  16. sum+=a[i]*i;
  17. }
  18. if(sum<=)break; //没有弹珠,即最后一行的情况
  19. memset(dp,,sizeof(dp));
  20. printf("Collection #%d:\n",cs++);
  21. if(sum%)printf("Can't be divided.\n");//奇数一定不可分
  22. else
  23. {
  24. dp[]=; //初始化0,0肯定是可分出来的
  25. sum/=; //背包容量
  26. for(i=; i<=; i++)
  27. {
  28. if(a[i]==)continue;
  29. for(k=; k<=a[i]; k*=)//先处理偶数情况,将偶数分值归到dp中,倒序同样是因为还原成了0-1背包问题,将大物品拆分成了各个小物品
  30. {
  31. for(j=sum; j>=; j--)
  32. {
  33. if(dp[j]==||j+i*k>sum)continue;
  34. dp[j+i*k]=;
  35. }
  36. a[i]-=k;
  37. }
  38. if(a[i]>)//处理剩余的数的情况,比如10分为1,2,4之后还剩3
  39. {
  40. for(j=sum; j>=; j--)
  41. {
  42. if(dp[j]==||j+i*a[i]>sum)continue;
  43. dp[j+i*a[i]]=;
  44. }
  45. }
  46. }
  47. if(dp[sum]==)
  48. printf("Can be divided.\n");
  49. else printf("Can't be divided.\n");
  50. }
  51. printf("\n");
  52. }
  53. return ;
  54. }

POJ 1014 Dividing(入门例题一)的更多相关文章

  1. POJ 1014 Dividing(多重背包+二进制优化)

    http://poj.org/problem?id=1014 题意:6个物品,每个物品都有其价值和数量,判断是否能价值平分. 思路: 多重背包.利用二进制来转化成0-1背包求解. #include&l ...

  2. DFS(DP)---POJ 1014(Dividing)

    原题目:http://poj.org/problem?id=1014 题目大意: 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两 ...

  3. POJ 1014 Dividing

    Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 66032 Accepted: 17182 Descriptio ...

  4. POJ 1014 Dividing(多重背包)

    Dividing   Description Marsha and Bill own a collection of marbles. They want to split the collectio ...

  5. POJ 1014 Dividing 多重背包

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63980   Accepted: 16591 Descri ...

  6. Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)

    多重背包模板- #include <stdio.h> #include <string.h> int a[7]; int f[100005]; int v, k; void Z ...

  7. POJ 1014 Dividing (多重可行性背包)

    题意 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两份的总价值相等,其中一个物品不能切开,只能分给其中的某一方,当输入六个0是( ...

  8. POJ 1014 Dividing(多重背包, 倍增优化)

    Q: 倍增优化后, 还是有重复的元素, 怎么办 A: 假定重复的元素比较少, 不用考虑 Description Marsha and Bill own a collection of marbles. ...

  9. POJ 1014 Dividing 背包

    二进制优化,事实上是物体的分解问题. 就是比方一个物体有数量限制,比方是13,那么就须要把这个物体分解为1. 2, 4, 6 假设这个物体有数量为25,那么就分解为1, 2, 4. 8. 10 看出规 ...

随机推荐

  1. 超详细的Hadoop2配置详解

    1. 集群环境 Master 192.168.2.100 Slave1 192.168.2.101 Slave2 192.168.2.102 2. 下载安装包 Master wget http://m ...

  2. tensorflow API _ 3 (tf.train.polynomial_decay)

    学习率的三种调整方式:固定的,指数的,多项式的 def _configure_learning_rate(num_samples_per_epoch, global_step): "&quo ...

  3. K-Nearest Neighbors Algorithm

    K近邻算法. KNN算法非常简单,非常有效.KNN算法适合样本较少典型性较好的样本集. KNN的模型表示是整个训练数据集.也可以说是:KNN的特点是完全跟着数据走,没有数学模型可言. 对一个新的数据点 ...

  4. ajax 样式

    Ajax 由 HTML.JavaScript™ 技术.DHTML 和 DOM 组成,这一杰出的方法可以将笨拙的 Web 界面转化成交互性的 Ajax 应用程序.它是一种构建网站的强大方法. 使用aja ...

  5. Xamarin移动开发之路

    Xamarin入门 1.Xamarin开发及学习资源 2.Xamarin安装及调试 Xamarin.Forms 1.入门 [快速入门] 2.XAML 3.应用程序基础知识 [辅助功能]eg:大类型.高 ...

  6. 通过redash query results 数据源实现跨数据库的查询

    redash 提供了一个简单的 query results 可以帮助我们进行跨数据源的查询处理 底层数据的存储是基于sqlite的,期望后期有调整(毕竟处理能力有限),同时 query results ...

  7. jQuery - 添加元素append/prepend和after/before的区别

    append <p> <span class="s1">s1</span> </p> <script> $(" ...

  8. python 判断一个字符串组合后,是否在另一个字符串中

    code #coding=utf- def getdic(s): dic = {} for i in s: if (i not in dic): dic[i] = else: dic[i] += re ...

  9. webpack系列之安装(Mac OS)

    1. webpack介绍,可参考Webpack中文文档 2. 安装webpack之前先需要安装npm,可参看NPM的使用介绍 3. 安装webpack,可参考Webpack入门教程 ========= ...

  10. Jmeter(四十二)_控制器下遍历一组参数

    概述 在接口自动化的过程中,经常遇到需要遍历的参数组.jmeter在中,foreach控制器可以实现遍历参数,但是只能有一个入参.一旦遇到数组,foreach控制器表示我也无能为力... 为了解决这个 ...