Dividing

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

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

题意:
        价值分别为1,2,3,4,5,6的6件物品,每件有ni件(ni<=20000),问是否能分成等价值的两份!
       较好的动态规划模板题!
代码:
 #include "stdio.h"
#include "string.h"
#define N 120005
int a[],map[N];
int sum; int MAX(int x,int y)
{
if(x>y) return x;
return y;
} void CompletePack(int cost,int weight) //完全背包
{
for(int i=cost;i<=sum;i++)
map[i] = MAX(map[i],map[i-cost]+weight);
} void ZeroOnePack(int cost,int weight) // 01背包
{
for(int i=sum;i>=cost;i--)
map[i] = MAX(map[i],map[i-cost] + weight);
} void dp(int cost,int weight,int k)
{
if(cost*k>=sum)
CompletePack(cost,weight);
else
{
for(int j=;j<k; ) //二进制优化
{
ZeroOnePack(j*cost,j*weight);
k-=j;
j*=;
}
ZeroOnePack(k*cost,k*weight);
}
} int main()
{
int i,p=;
while()
{
sum=;
for(i=;i<=;i++)
{
scanf("%d",&a[i]);
sum+=a[i]*i;
}
if(sum==) return ;
printf("Collection #%d:\n",p++);
if(sum%==)
{
printf("Can't be divided.\n\n");
continue;
}
sum=sum/;
memset(map,,sizeof(map));
for(i=;i<=;i++)
dp(i,i,a[i]);
if(map[sum]==sum)
printf("Can be divided.\n\n");
else
printf("Can't be divided.\n\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 & Zoj 1149 & poj 1014 Dividing(多重背包)

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

  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

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

  8. HDU 1059 Dividing(多重背包)

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

  9. HDU 1059 Dividing (dp)

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

随机推荐

  1. C#基本概念列举说明

    1. 关键字      在C#代码中常常使用关键字,关键字也叫保留字,是对C#有特定意义的字符串.关键字在Visual Studio 环境的代码视图中默认以蓝色显示.例如,代码中的using.name ...

  2. Flask and uWSGI - unable to load app 0 (mountpoint='') (callable not found or import error)

    Here is my directory structure: -/path/to/folder/run.py -|app -|__init__.py -|views.py -|templates - ...

  3. window下安装redis

    以cmd安装方法: .下载安装包:https://github.com/dmajkic/redis/downloads .安装包下载后根据操作系统选择对应版本文件,里面会有几个dll分别为: redi ...

  4. 技巧题---Single boy

    Description Today is Christmas day. There are n single boys standing in a line. They are numbered fo ...

  5. 一个SpringMVC简单Demo中出现的错误

    最近在学springmvc 一个简答的Springmvc配置包括如下步骤: 1.在 web.xml 文件中配置 DispatcherServlet (该中央控制器相当于 MVC 模式中的 C),还可以 ...

  6. 深入.NET框架

    .NET是微软公司在2000年推出的一个战略(平台). 其目的就是想 任何人使用任何终端设备在任何地方都可以访问微软提供的服务. .NET Framework两大组件: CLR(Common Lang ...

  7. ahjesus linux连接阿里云ubuntu服务器并更改默认账号和密码,以及创建子账户

    先确保本地Linux服务器SSH服务开启,如果没有开启直接执行指令:service sshd start 然后我们使用ssh指令进行远程登陆 ssh username@ip-address 输入pas ...

  8. DWR的Reverse Ajax技术实现

    DWR的逆向ajax其实主要包括两种模式:主动模式和被动模式.其中主动模式包括Polling和Comet两种,被动模式只有Piggyback这一种. 所谓的Piggyback指的是如果后台有什么内容需 ...

  9. Cookies简介和使用

    Cookie public class javax.servlet.http.Cookie  1作用(1)Cookie能使站点跟踪特定访问者的访问次数.最后访问时间和访问者进入站点的路径(2)Cook ...

  10. django性能优化

    1. 内存.内存,还是加内存 2. 使用单独的静态文件服务器 3. 关闭KeepAlive(如果服务器不提供静态文件服务,如:大文件下载) 4. 使用memcached 5. 使用select_rel ...