题目链接:http://www.tzcoder.cn/acmhome/problemdetail.do?method=showdetail&id=1315
描述

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.

输入

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.

输出

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.

思路:
题意大致为有权值为1~6的6中珠子给定每种的个数,问是否能分成权值相同的两份。一开始想错地方了,想要如何分成两堆相同的。后来一想只要分成两份,那如果能分成一堆权值和为所有珠子权值和的一半,那剩下的也是一半……
求可行性直接上背包。(不过要二进制优化下)

#include<bits/stdc++.h>
using namespace std;
const int Ms=5e5+5;
bool dp[Ms];
int main()
{int a[7],no=1;while(1){int sum=0,k=0;for(int i=1;i<=6;i++){scanf("%d",&a[i]);sum+=a[i];k+=i*a[i];}if(sum==0)break;int f=0;if(k&1)f=1;if(!f){k>>=1;memset(dp,0,k+1);dp[0]=1;for(int i=1;i<=6;i++){for(int j=1;j<=a[i]&&j*i<=k;j<<=1){for(int w=k;w>=j*i;w--){dp[w]|=dp[w-j*i];}if(dp[k])break;}if(dp[k])break;}if(!dp[k])f=1;}printf("Collection #%d:\n",no++);if(f)printf("Can't be divided.\n\n");else printf("Can be divided.\n\n");}return 0;
}

若有什么错误,欢迎指正^ _ ^ 。

Dividing(背包)相关推荐

  1. POJ 1014 Dividing 背包

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

  2. 【POJ】 1014 Dividing(多重背包,优化)

    [POJ] 1014 Dividing(多重背包,优化) [题目链接]http://poj.org/problem?id=1014 题目 Description Marsha and Bill own ...

  3. dividing(多重背包)

    E - Dividing HDU - 1059 有6种宝石,价值分别为1,2,3,4,5,6. 现在有一个长度为6的数组,第i个数字表示价值为i的宝石的数量. 例如:[1,0,1,2,0,0]表示有一 ...

  4. Dividing 多重背包 倍增DP

    Dividing 给出n个物品的价值和数量,问是否能够平分. 1 #include <iostream> 2 #include <cstring> 3 #include < ...

  5. POJ 1014 Dividing(多重背包 + 倍增优化)

    题意: 求一种划分方案使结果最公平. 思路: 多重背包,然后把物品分为 2^0, 2^1, ... , 2^k,... 等. #include <iostream> #include &l ...

  6. [多重背包+二进制优化]HDU1059 Dividing

    题目链接 题目大意: 两个人要把一堆宝珠,在不能切割的情况下按照价值平分,他们把宝珠分成6种价值,每种价值的宝珠n个. n<=200000 思考: 首先如果加和下来的价值是一个偶数 那么还分毛啊 ...

  7. (step3.3) hdu 1059(Dividing——多重背包)

    题目大意:分别给出价值为1~6的石头的数量.问能否将这些石头等价值平分... 解题思路:多重背包 1)多重背包的典型描述是这样的:给出n种物品,背包的容量为V.每种物品的可用数量为num[i],所占体 ...

  8. HDU 1059 Dividing 多重背包

    这题想了想用母函数应该也可以做,不过得考虑一个细节,就是加起来的总和是否为奇数,我找了好久一直没找出错误,原来是没考虑奇数..  fuck  这种问题我怎么能不考虑奇数呢.....  啊 啊 啊啊 啊 ...

  9. POJ 1014 Dividing【多重背包+二进制优化】

    大意: 价值1, 2, 3, --, 6的物品分别a1, a2, --, a5, a6件 问能否把这些物品分成两份,使其具有相同的价值(所有物品必须全部用上) 分析: 给个物品有多件,即多重背包 只要 ...

  10. uva562 - Dividing coins(01背包)

    题意: 给你一堆硬币,让你分成两堆,分别给A,B两个人,求两人得到的最小差. 思路: 这题可以用背包写,只要把背包总重量当做sum/2就好,然后就看能选硬币最接近却又不超过sum/2的值为多少.这题原 ...

最新文章

  1. 重新认识vue之事件阻止冒泡
  2. typedef与#define宏区别
  3. 【工具推荐】Hadoop集群监控工具 HTools
  4. 标C编程笔记day04 预处理、宏定义、条件编译、makefile、结构体使用
  5. Antlr中文文档初稿2(《ANTLR树分析器》)
  6. 网页连接数据库 服务器,关于asp网页连接远程服务器上数据库问题
  7. stm32F4的ADC+DMA+Timer,实现2MHz连续采样。1LSB分辨率,极低噪声。
  8. python怎么读excelsheet_python怎么读写excel文件
  9. getset原子性 redis_RedisAPI原子性操作及原理解析
  10. spring管理bean容器(笔记)[继]
  11. 在excel 同时冻结行和列
  12. vue实现PC端和移动端的界面切换
  13. 【STM32F407的DSP教程】第28章 FFT和IFFT的Matlab实现(幅频响应和相频响应)
  14. H5外包团队:使用HTML5播放短视频代码分享
  15. Gitlab 12.9.4 的搭建部署及遇到的问题。
  16. HDU 4735 舞蹈链可重复覆盖
  17. linux重新启动apache,如何启动,停止或重新启动Apache
  18. 华北电力大学控制与计算机工程学院老师,华北电力大学控制与计算机工程学院导师教师师资介绍简介-杨静...
  19. JDK1.8u安装教程
  20. 振动位移传感器IEPE加速度3轴采集模块

热门文章

  1. unity中游戏对象的运动控制方法总结
  2. C# WinForm 无边框窗体,加阴影、拖动、改变大小等功能完美实现(自认为是完美的 ^=^)
  3. Access denied for user 'root'@'localhost' (using password: NO)
  4. [每日一题] 62. 美国节日(日期计算、蔡勒公式)
  5. 【MMDet Note】MMDetection中AnchorGenerator代码理解与解读
  6. .net分布式压力测试工具(Beetle.DT)
  7. mysql.exe不运行_求解mysql进程mysqld.exe无法启动原因及解决办法
  8. pl/sql developer oracle生僻字显示问题
  9. MINI LED 驱动IC
  10. PCI Express学习篇:Power Management(一)