点击打开链接链接

Dividing

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

Total Submission(s): 17544    Accepted Submission(s): 4912

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
 
Recommend
JGShining

给出价值分别为1 2 3 4 5 6 的石头的个数 问能不能平分

一个容量为sum/2的背包

#include<cstdio>
#include<cstring>
int maxn(int a,int b)
{
return a>b?a:b;
}
int cnt[10];
int w[22222];
int dp[22222];
int main()
{
int sum;
int cas=1;
int xx;
while(scanf("%d",&cnt[0])!=EOF)
{
sum=cnt[0];
for(int i=1;i<6;i++)
{
scanf("%d",&cnt[i]);
sum+=cnt[i]*(i+1);
}
if(!sum)
break;
xx=0;
for(int i=0;i<6;i++)
{
for(int j=1;j<=cnt[i];j*=2)
{
w[xx++]=j*(i+1);
cnt[i]-=j;
}
if(cnt[i])
{
w[xx++]=cnt[i]*(i+1);
cnt[i]=0;
}
}
printf("Collection #%d:\n",cas++);
//printf("%d\n",sum);
if(sum%2)
{
printf("Can't be divided.\n");
}
else
{
memset(dp,0,sizeof(dp));
for(int i=0;i<xx;i++)
{
for(int j=sum/2;j>=w[i];j--)
{
dp[j]=maxn(dp[j],dp[j-w[i]]+w[i]);
}
}
//printf("%d\n",dp[sum/2]);
if(dp[sum/2]==sum/2)
{
printf("Can be divided.\n");
}
else
{
printf("Can't be divided.\n");
}
}
puts("");
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

hdu 1059 Dividing 多重背包的更多相关文章

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

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

  2. POJ 1014 / HDU 1059 Dividing 多重背包+二进制分解

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

  3. HDU 1059(多重背包加二进制优化)

    http://acm.hdu.edu.cn/showproblem.php?pid=1059 Dividing Time Limit: 2000/1000 MS (Java/Others)    Me ...

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

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

  5. hdu 1059 Dividing bitset 多重背包

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

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

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

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

  8. ACM学习历程—HDU 1059 Dividing(dp && 多重背包)

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

  9. 题解报告:hdu 1059 Dividing(多重背包、多重部分和问题)

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

随机推荐

  1. linux笔记本上安装了双显卡驱动(intel+nvidia)

    为了提高linux图形性能并降低功耗,独特的文章. 我用的系统Fedora 20 Xfce x64,在安装驱动程序,以确保系统是最新的版本号. 最好安装gcc.kernel-devel和其他包.己主动 ...

  2. SQLServer-----使用jTDS连接SQLServer数据库

    一.jTDS一个简短的引论 jTDS100%纯Java实现的JDBC3.0驱动,它用于连接 Microsoft SQL Server(6.5.7.2000,2005,2008 和 2012)和Syba ...

  3. MLAPP——概率机器学习知识汇总

    <机器学习>课程使用Kevin P. Murphy图书<Machine Learning A Probabilistic Perspective>本英语教材,本书从一个独特的数 ...

  4. 2014 Multi-University Training Contest 1/HDU4861_Couple doubi(数论/法)

    解题报告 两人轮流取球,大的人赢,,, 贴官方题解,,,反正我看不懂.,,先留着理解 关于费马小定理 关于原根 找规律找到的,,,sad,,, 非常easy找到循环节为p-1,每个循环节中有一个非零的 ...

  5. swift UI特殊培训38 与滚动码ScrollView

    有时我们适合页面的全部内容,我们需要使用ScrollView,额外的内容打通滚动. 什么样的宽度和高度首先,定义,健身器材轻松. let pageWidth = 320 let pageHeight ...

  6. Unity3D 如何图形问题修正旋转模型已导入?

     如何纠正旋转模型被导入? 一些立体艺术资源包导出其模式,以便 Z 轴向上.Unity 大多数标准的脚本中假定的三维世界 Y 轴代表了.在 Unity 比改动脚本使其契合easy得多. Z 轴朝上 ...

  7. Javascript学习2 - Javascript中的表达式和运算符

    原文:Javascript学习2 - Javascript中的表达式和运算符 Javascript中的运算符与C/C++中的运算符相似,但有几处不同的地方,相对于C/C++,也增加了几个不同的运算符, ...

  8. 深入理解ASP.NET MVC Day1

    深入理解ASP.NET MVC   ASP.NET vs MVC vs WebForms 许多ASP.NET开发人员开始接触MVC认为MVC与ASP.NET完全没有关系,是一个全新的Web开发,事实上 ...

  9. Java串口通信详细解释

    前言 说到开源.恐怕非常少有人不挑大指称赞. 学生通过开源码学到了知识,程序猿通过开源类库获得了别人的成功经验及可以按时完毕手头的project,商家通过开源软件赚到了钱……,总之是皆大欢喜. 然而开 ...

  10. git_自动同步_sync.sh

    1: 使用方法  sh sync.sh  code_dir 代码 #add echo $1 cd $1 git add . git commit -m "Added notes for $( ...