Dividing
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 63013   Accepted: 16315

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 file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , 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 collection, 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.

给六种宝石,价值分别是1 2 3 4 5 6,题中input给的是各个宝石的数量,要求的是能不能将这些数量的宝石,分成等价值的两堆。

要是别人不告诉我这是动态规划题,我真想不出这种思路。

头一次见过这种思路,求整体能不能分开,是看其一半值total在数组DP[total]中是不是为true,说是动态规划,其动态规划就体现在在取第n轮宝石时,只跟n-1轮宝石的值有关,这和POJ2392那道题的思路很类似很类似。只要理解思路,写出代码就不难了。

代码:

<span style="font-size:14px;">#include <iostream>
#include <vector>
using namespace std; int dp[8][120002];//dp[i][j]代表选第i中宝石时,j是否可能取到
//dp[i][j]=dp[i-1][j-i*x]
int marible[8];
int total=0; void init()
{
int count;
total=0;
for(count=1;count<=6;count++)
{
cin>>marible[count];
total += marible[count]*count;
}
} void cal()
{
int i,j,x; memset(dp,0,sizeof(dp));
dp[1][0]=1;
for(i=1;i<=marible[1];i++)
dp[1][i]=1; for(i=2;i<=6;i++)
{
for(j=0;j<=total;j++)
{
if(dp[i-1][j])
{
for(x=1;x<=marible[i];x++)
{
dp[i][j+x*i]=dp[i-1][j];
}
}
if(dp[i][j])
continue;
dp[i][j]=dp[i-1][j]; }
} } int main()
{
bool end = true;
int t=0;
while(end)
{
t++; init();
if(total==0)
{
return 0;
}
if(total%2)
{
cout<<"Collection #"<<t<<":"<<endl;
cout<<"Can't be divided."<<endl<<endl;
}
else
{
total /=2;
cal();
if(dp[6][total])
{
cout<<"Collection #"<<t<<":"<<endl;
cout<<"Can be divided."<<endl<<endl;
}
else
{
cout<<"Collection #"<<t<<":"<<endl;
cout<<"Can't be divided."<<endl<<endl;
}
}
}
return 0;
}</span>

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ1014:Dividing的更多相关文章

  1. 【poj1014】 Dividing

    http://poj.org/problem?id=1014 (题目链接) 题意 给出有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份, ...

  2. poj1014:母函数+优化

    题目大意: 有1~6六种宝石,价格分别为1~6 ..给定每种宝石的个数,问能否平分给两个人 分析: 一看显然是个多重背包问题,也可以用母函数做 不过母函数的复杂度是n*v*k,第一次tle了.. 后来 ...

  3. poj1014 hdu1059 Dividing 多重背包

    有价值为1~6的宝物各num[i]个,求能否分成价值相等的两部分. #include <iostream> #include <cstring> #include <st ...

  4. 题解 【POJ1014】 Dividing

    题目意思 有六种不同的石子,权值为\(1\)~\(6\),给出六种石子的数量,求能否将石子分成权值相等的两份. 解析 这题可以直接用多重背包写, 因为仔细想想, 能够平均分成两份, 也就是能将一部分石 ...

  5. Command and Query Responsibility Segregation (CQRS) Pattern 命令和查询职责分离(CQRS)模式

    Segregate operations that read data from operations that update data by using separate interfaces. T ...

  6. 多重背包 (poj 1014)

    题目:Dividing 题意:6种重量的的石头,每个给定数量,用总重的一半去装,问能否装满. #include <iostream> #include <algorithm> ...

  7. hdu 4963(中途相遇法)

    题目链接:Dividing a String 题意:给定一个2*n(n<=20)的字符串及每个位置的字符包含的权重,求将该字符串分成两个子序列S1.T1,要求S1=T1且abs(weight1- ...

  8. java web 开发三剑客 -------电子书

    Internet,人们通常称为因特网,是当今世界上覆盖面最大和应用最广泛的网络.根据英语构词法,Internet是Inter + net,Inter-作为前缀在英语中表示“在一起,交互”,由此可知In ...

  9. 所有selenium相关的库

    通过爬虫 获取 官方文档库 如果想获取 相应的库 修改对应配置即可 代码如下 from urllib.parse import urljoin import requests from lxml im ...

随机推荐

  1. 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)

    题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...

  2. electron-edge-js 环境搭建

    确保nodejs环境 为 10.X  (因为edge的编译需要node对应版本的支持,太新的node不包含对应edge的编译) 1.创建工程2.使用npm init初始化程序信息3.使用npm ins ...

  3. 跨服务器的SQL语句如何书写

    select  *  into  本地库名..表名  from  OPENDATASOURCE(                  'SQLOLEDB',                  'Data ...

  4. zabbix通过ipmi传感器监控浪潮服务器的硬件信息

    一:实验对象 操作系统版本:centos7.6 监控对象:通过服务器传感器获取到的所有在使用的硬件信息 zabbix版本: 4.0.14二:zabbix介绍 zabbix适合中小型企业.大型企业的用户 ...

  5. 二 基于java动态数组手写栈

    package dataStucture2.stack; import dataStucture2.array.MyDynamicArray; /** * 基于动态数组手写栈 * 设计时,栈中仅栈顶对 ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:表格单元格使用了 "bg-danger" 类

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. IDEA配置数据库连接失败的问题

    今天采用IDEA连接数据库失败了,有几个问题需要注意 首先笔者采用的数据库版本为8.0.17而IDEA自带版本是5.2.26大概,于是首先出现的问题是驱动不匹配,那么就需要换成我自己的版本,配置如下 ...

  8. 小程序行内元素且有border的情况下,根据文字宽度自动调节元素宽度

    比如认识成都: <view style="display: flex;align-items: center;"> <text class="cityV ...

  9. SciPy 特殊函数

    章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...

  10. hdu 3549 Flow Problem 最大流问题 (模板题)

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...