点击打开链接链接

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. 【Nginx】显示器port管理

    监听port属于server虚拟主机,由server{}块内的listen配置项决定. 也就是说,在server{}块配置项内定义了该虚拟主机所要监听的port. 在处理配置文件http块内main级 ...

  2. 使用javascript实现html文字不可选

    如何使用js让html该文本是不可选定它?首先想到的是用css选择实现,如下面: -webkit-touch-callout: none; -webkit-user-select: none; -kh ...

  3. SQL 注意事项

    -------选择表名 配置Ctrl+3 能够select * 桌 USE [NB] GO /* 物: StoredProcedure [dbo].[SP_Select] 脚本日期: 05/28/20 ...

  4. ISA TEST Writeup

    刚出来的hack游戏,非常easy,现在只有7关.考虑入门级.没有什么可以玩. http://helloisa.com/ LEVEL 1 细致观察页面,入侵的第一步是收集一切可能产生价值的信息 ps: ...

  5. 考试easy该,学习如何做?

    我的两个学生(场和任)都讲了他们周末參加的一个认证考试不考大题考小题的事情.由感而发: 话说不用大题考,大概是不敢用大题考. 老师的教.和学生的学中.存在的一些问题得不到解决,整体讲,学生的学习效果没 ...

  6. 【年终分享】彩票数据预测算法(一):离散型马尔可夫链模型实现【附C#代码】

    原文:[年终分享]彩票数据预测算法(一):离散型马尔可夫链模型实现[附C#代码] 前言:彩票是一个坑,千万不要往里面跳.任何预测彩票的方法都不可能100%,都只能说比你盲目去买要多那么一些机会而已. ...

  7. 从源码看Android中sqlite是怎么读DB的(转)

    执行query 执行SQLiteDatabase类中query系列函数时,只会构造查询信息,不会执行查询. (query的源码追踪路径) 执行move(里面的fillwindow是真正打开文件句柄并分 ...

  8. thinkphp学习笔记4—眼花缭乱的配置

    原文:thinkphp学习笔记4-眼花缭乱的配置 1.配置类别 ThinkPHP提供了灵活的全局配置功能,ThinkPHP会依次加载管理配置>项目配置>调试配置>分组配置>扩展 ...

  9. C# Windows Schedule task此次收购task下一步执行时间

    最近进行了一次需求和Windows Schedule task相关职能,通过schedule,计算下一次运行task时间. 它用于由第三方DLL实现,以下网站,以下载来自: http://tasksc ...

  10. ENode框架初始化

    ENode框架初始化 前言 Conference案例是使用ENode框架来开发的.之前我没有介绍过ENode框架是如何启动的,以及启动时要注意的一些点,估计很多人对ENode框架的初始化这一块感觉很复 ...