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

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
 思路:之前还没看多重背包,就直接把每种物品分为 (背包大小/费用)件,然,我们并不用把每种物品分为费用和权值都相同的多件,为尽量减少分的件数,可每件的费用为 c[i]×2^k, 价值为 w[i]×2^k .......n[i] - 2^k + 1, 如n[i] = 13 时, 可分为 1 , 2,  4,  6  作为系数分别乘以 c[i],  w[i]
 #include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std ;
int dp[] ;
int r[] ;
int sum, num ;
int c = ;
void _zeroone(int c, int w)//0-1背包处理单件物品
{
for(int i = sum; i >= c; --i)
dp[i] = max(dp[i], dp[i - c] + w) ;
}
void _compl(int c, int w)//完全背包处理单件物品
{
for(int i = c; i <= sum; ++i)
dp[i] = max(dp[i], dp[i - c] + w) ;
}
void _mult(int c, int w, int amount)
{
if(c * amount >= sum) { _compl(c, w) ; return ; }//满足条件时,该件物品可以直接当完全背包时的单件物品处理
int k = ;
while(k < amount)//二进制分解
{
_zeroone(c * k,w * k) ;
amount -= k ;
k = k * ;
}
_zeroone(amount * c, amount * w) ;
}
int main()
{
while()
{
memset(dp, , sizeof dp) ;
sum = ;
int flag = ;
for(int i = ; i <= ; ++i){
scanf("%d",&r[i]) ;
if(r[i] != ) flag = ;
sum += i * r[i] ;
}
if(!flag) break ;
printf("Collection #%d:\n",c++) ;
if(sum & ) { printf("Can't be divided.\n\n") ; continue ;}
sum /= ;
for(int i = ; i <= ; ++i)//将多重背包每件分开转换为完全背包或0-1背包中的单件物品处理
_mult(i,i,r[i]) ;
if(dp[sum] == sum ) printf("Can be divided.\n") ;
else printf("Can't be divided.\n") ;
printf("\n") ;
}
return ;
}
 

hdu 1059 Dividing的更多相关文章

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

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

  2. hdu 1059 Dividing bitset 多重背包

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

  3. 动态规划--模板--hdu 1059 Dividing

    Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  4. hdu 1059 Dividing 多重背包

    点击打开链接链接 Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. HDU 1059 Dividing 分配(多重背包,母函数)

    题意: 两个人共同收藏了一些石头,现在要分道扬镳,得分资产了,石头具有不同的收藏价值,分别为1.2.3.4.5.6共6个价钱.问:是否能公平分配? 输入: 每行为一个测试例子,每行包括6个数字,分别对 ...

  6. hdu 1059 Dividing(多重背包优化)

    Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  7. HDU 1059 Dividing(多重背包)

    点我看题目 题意: 将大理石的重量分为六个等级,每个等级所在的数字代表这个等级的大理石的数量,如果是0说明这个重量的大理石没有.将其按重量分成两份,看能否分成. 思路 :一开始以为是简单的01背包,结 ...

  8. 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 ...

  9. HDU 1059 Dividing (dp)

    题目链接 Problem Description Marsha and Bill own a collection of marbles. They want to split the collect ...

随机推荐

  1. Python简易聊天工具-基于异步Socket通信

    继续学习Python中,最近看书<Python基础教程>中的虚拟茶话会项目,觉得很有意思,自己敲了一遍,受益匪浅,同时记录一下. 主要用到异步socket服务客户端和服务器模块asynco ...

  2. iOS小技巧总结,绝对有你想要的

    原文链接 在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新. UITableView的Group样式下顶部空白处理 //分组列表头部空白处理 UIView *view = [[UIV ...

  3. 从键盘输入成绩,找出最高分,并输出学生成绩等级。成绩>=最高分-10,为A,成绩>=最高分-20,为B,成绩>=最高分-30,为C,其余等级为D

    import java.util.Scanner;public class TestStudent { public static void main(String[] args) { //从键盘获得 ...

  4. Oracle读写分离架构

    读写分离是架构分布式系统的一个重要思想.不少系统整体处理能力并不能同业务的增长保持同步,因此势必会带来瓶颈,单纯的升级硬件并不能一劳永逸.针对业务类型特点,需要从架构模式上进行一系列的调整,比如业务模 ...

  5. Navicat 回复 psc 文件 Mysql

    在mysql 中回复 psc文件 的时候 只能一步步来,先在navicat中建一个空数据库,然后点击有上角的备份==>回复备份==> 找到psc文件==> 注意此时不要急于点击 开始 ...

  6. NYOJ题目457大小写互换

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsUAAAIUCAIAAAB9y8bFAAAgAElEQVR4nO3dPW7bTNsG0G8T7r0Qt/

  7. UISegmentedControl

    1. NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3& ...

  8. Android Programming: Pushing the Limits -- Chapter 7:Android IPC -- ApiWrapper

    前面两片文章讲解了通过AIDL和Messenger两种方式实现Android IPC.而本文所讲的并不是第三种IPC方式,而是对前面两种方式进行封装,这样我们就不用直接把Aidl文件,java文件拷贝 ...

  9. ManageEngine Glossary

    https://www.manageengine.com/products/applications_manager/help/glossary-applications-manager.html#s ...

  10. wifi diplasy流程介绍

    转自:http://blog.csdn.net/dnfchan/article/details/8558552/  另外一篇不错的参考文章:http://www.360doc.com/content/ ...