Dividing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22808    Accepted Submission(s): 6444

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.
 
Source
 
题解:6种石头拥有不同价值  现在要求你等分
        输入 n1...ni....n6  代表 价值为i的石头有ni个  若能等分按要求输出 反之亦然
 
题意:背包容量为总价值的一半  若能满足f[m]==m则说明能够等分
        完全背包的二进制优化实现
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define ll __int64
using namespace std;
int n[];
int m;
int jishu;
int size[];
int value[];
int coun=;
int f[];
void slove(int q)
{
coun=;
for(int i=;i<=q;i++)
{
int c=n[i],v=i;
for (int k=; k<=c; k<<=)
{
value[coun] = k*v;
size[coun++] = k*v;
c -= k;
}
if (c > )
{
value[coun] = c*v;
size[coun++] = c*v;
}
}
}
int main()
{ jishu=;
while(scanf("%d %d %d %d %d %d",&n[],&n[],&n[],&n[],&n[],&n[])!=EOF)
{
if(n[]==&&n[]==&&n[]==&&n[]==&&n[]==&&n[]==)
break;
memset(f,,sizeof(f));
memset(size,,sizeof(size));
memset(value,,sizeof(value));
m=n[]+n[]*+n[]*+n[]*+n[]*+n[]*;
if(m%)
{
printf("Collection #%d:\nCan't be divided.\n\n",++jishu);
}
else
{
slove();
m=m/;
for(int i=;i<=coun-;i++)
for(int k=m;k>=value[i];k--)
{
f[k]=max(f[k],f[k-size[i]]+value[i]);
}
if(f[m]==m)
printf("Collection #%d:\nCan be divided.\n\n",++jishu);
else
printf("Collection #%d:\nCan't be divided.\n\n",++jishu); } }
return ;
}
 
 
 
 

HDU 1059 完全背包的更多相关文章

  1. hdu 1059 (多重背包) Dividing

    这里;http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意是有价值分别为1,2,3,4,5,6的商品各若干个,给出每种商品的数量,问是否能够分成价值相等的 ...

  2. hdu 1059 多重背包

    题意:价值分别为1,2,3,4,5,6的物品个数分别为a[1],a[2],a[3],a[4],a[5],a[6],问能不能分成两堆价值相等的. 解法:转化成多重背包 #include<stdio ...

  3. Dividing (hdu 1059 多重背包)

    Dividing Sample Input 1 0 1 2 0 0 价值为1,2,3,4,5,6的物品数目分别为 1 0 1 2 0 0,求能否将这些物品按价值分为两堆,转化为多重背包.1 0 0 0 ...

  4. D - D 分糖果HDU - 1059(完全背包+二进制优化)

    有两个小朋友想要平分一大堆糖果,但他们不知道如何平分需要你的帮助,由于没有spj我们只需回答能否平分即可. 糖果大小有6种分别是1.2.3.4.5.6,每种若干颗,现在需要知道能不能将这些糖果分成等额 ...

  5. hdu 1059 多重背包 背包指数分块

    思路: 这个方法要看<浅谈几类背包问题>这篇论文. #include"stdio.h" #define Max(a,b) (a)>(b)?(a):(b) ],k[ ...

  6. HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)

    HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...

  7. hdu 1059 Dividing bitset 多重背包

    bitset做法 #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a ...

  8. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  9. hdu 5445 多重背包

    Food Problem Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)To ...

随机推荐

  1. Leecode刷题之旅-C语言/python-13.罗马数字转整数

    /* * @lc app=leetcode.cn id=13 lang=c * * [13] 罗马数字转整数 * * https://leetcode-cn.com/problems/roman-to ...

  2. 隐藏WPF ToolBar 左侧的移动虚线和右侧的小箭头

    原文:隐藏WPF ToolBar 左侧的移动虚线和右侧的小箭头   上面的图是两个工具栏的链接处.   去除蓝色部分的方法是 设置工具栏的ToolBarTray.IsLocked附加选项为True   ...

  3. Android ANR 分析

    首先贴一下trace 文件 Process: com.oppo.reader PID: 20358 Time: 2933175644_1545041895232 Flags: 0x38d83e44 P ...

  4. Bit-map法处理大数据问题

    问题引入: 1.给40亿个不重复的unsigned int的整数,没排过序的,然后再给一个数,如何快速判断这个数是否在那40亿个数当中?2.给定一个千万级别数据量的整数集合,判断哪些是重复元素.3.给 ...

  5. Django笔记 —— 高级视图和URL配置

    最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...

  6. MySQL☞聚合函数/分组函数

    分组函数(聚合函数) 1.count(*/列名): a.*:求出该数据的总条数 select  count(*)  from 表名 b.列名:求出该列中列名不为null的总条数 select  cou ...

  7. Tuxedo 通讯方式解析

    本节根据tuxedo自带samples的例子,让其运行起来.并通过这个例子,深入的理解tuxedo的通讯方式. 进入tuxedo的安装目录,samples目录下自带了一些例子 [root@localh ...

  8. python学习总结----异常处理

    相关概念 - 错误:程序运行之前的语法错误,如:关键字.缩进不齐.括号不成对. - 异常:在程序运行过程中出现的问题,如:除数为0.对象属性不存在等. 异常处理 - 说明:异常处理可以理解为特殊的流程 ...

  9. 【转】H5 - HTML5新增标签

    下面分别是传统的div+css的页面布局方式 下面是HTML5布局方式: 是不是精简了很多呢  现在来说说图片中出现的标签: 结构标签:(块状元素) 有意义的div artical 标记定义一篇文章  ...

  10. MySQL中Alter用法小结

    alter 方法是我们在处理MySQL数据库中一个常见的方法,能帮助我们更好的处理数据库中的表 1.增加 数据库中表的字段:alter table table_name add [column] co ...