POJ 1014 Dividing
Dividing
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 66032 Accepted: 17182
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的6种物品,输入6个数字,数字i代表价值为i的物品有ni个。问能否将物品分成等价的两份(物品当然不能拆开喽)。
输入:
每一行有六个数 分别代表价值为1,2,3,4,5,6的物品的个数,最大物品总数为20000.输入的结束标志为六个0.
输出:
对于第k个样例,输出“Collection #k:”,可以分成等价的两份则输出“Can be divided.”,不可以就输出“Can’t be divided.”。在每个样例后要记得要输出回车。
思路:
0/1背包的二进制优化。。
设一个变量all代表所有物品的价值和。如果all%2==1 则无论怎么分都不可能分成相等的两份。
如果在背包进行的过程中出现了dp[j]==all/2的情况,就可以被分成两份。(如果说的不清楚详情请见代码)。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[100],cases=0,dp[666666],f[666666],tot,all,w;
bool flag;
int main()
{
while(1)
{
cases++;
tot=0;flag=false;all=0;
memset(dp,0,sizeof(dp));
memset(f,0,sizeof(f));
for(int i=1;i<=6;i++) {scanf("%d",&a[i]);all+=a[i]*i;}
if(a[1]||a[2]||a[3]||a[4]||a[5]||a[6])
{
for(int i=1;i<=6;i++)
{
int t=1;
while(a[i]>=t)
{
tot++;
a[i]=a[i]-t;
f[tot]=t*i;
t*=2;
}
tot++;
f[tot]=a[i]*i;
}
if(all%2==1)goto end;
else w=all/2;
for(int i=1;i<=tot;i++)
{
for(int j=all;j>=f[i];j--)
{
dp[j]=max(dp[j],dp[j-f[i]]+f[i]);
if(dp[j]==w)flag=true;
}
}
if(!flag) end: printf("Collection #%d:\nCan't be divided.\n\n",cases);
else printf("Collection #%d:\nCan be divided.\n\n",cases);
}
else break;
}
}
POJ 1014 Dividing的更多相关文章
- POJ 1014 Dividing(多重背包+二进制优化)
http://poj.org/problem?id=1014 题意:6个物品,每个物品都有其价值和数量,判断是否能价值平分. 思路: 多重背包.利用二进制来转化成0-1背包求解. #include&l ...
- DFS(DP)---POJ 1014(Dividing)
原题目:http://poj.org/problem?id=1014 题目大意: 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两 ...
- POJ 1014 Dividing(多重背包)
Dividing Description Marsha and Bill own a collection of marbles. They want to split the collectio ...
- POJ 1014 Dividing 多重背包
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63980 Accepted: 16591 Descri ...
- 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 ...
- POJ 1014 Dividing (多重可行性背包)
题意 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两份的总价值相等,其中一个物品不能切开,只能分给其中的某一方,当输入六个0是( ...
- POJ 1014 Dividing(多重背包, 倍增优化)
Q: 倍增优化后, 还是有重复的元素, 怎么办 A: 假定重复的元素比较少, 不用考虑 Description Marsha and Bill own a collection of marbles. ...
- POJ 1014 Dividing 背包
二进制优化,事实上是物体的分解问题. 就是比方一个物体有数量限制,比方是13,那么就须要把这个物体分解为1. 2, 4, 6 假设这个物体有数量为25,那么就分解为1, 2, 4. 8. 10 看出规 ...
- POJ 1014 Dividing(入门例题一)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: Accepted: Description Marsha and Bill own ...
随机推荐
- 在centos环境安装mysql
在Linux上安装mysql数据库,我们可以去其官网上下载mysql数据库的rpm包,http://dev.mysql.com/downloads/mysql/5.6.html#downloads,大 ...
- php上传文件类型
下面提供一张IE和火狐浏览器的文件类型对照表: ie 火狐 id 后缀名 php识别出的文件类型 0 gif image/gif 1 jpg image/jpeg 2 png image/png 3 ...
- Spark机器学习读书笔记-CH05
5.2.从数据中提取合适的特征 [root@demo1 ch05]# sed 1d train.tsv > train_noheader.tsv[root@demo1 ch05]# lltota ...
- MFC-01-Chapter01:Hello,MFC---1.3 第一个MFC程序(01)
#include <afxwin.h> class CMyApp : public CWinApp { public: virtual BOOL InitInstance(); }; cl ...
- js 控制表单提交
<form id="form2"> <input type="text" name="text" value=" ...
- js实现全选反选功能
开始慢慢地学习js&jQuery. function clicked(){ var arr=document.getElementsByName("product"); f ...
- 我收录整理的优秀OC技术类文章
自定义导航按钮UIBarButtonItem 关于导航栏的六个小技巧 ios开发的一些小技巧篇一 制作一个可以滑动操作的 Table View Cell - IOS - 伯乐在线 一个 ...
- 拓扑排序&&欧拉(回)路
摘要:最近是不适合写代码么?忘记初始化wa到死<_=_=_>.唔--最近在学习图论,从基础搞起,先搞了拓扑排序和欧拉(回)路. Part 0. 拓扑排序 ==挖坑== Part 1. 欧拉 ...
- C# OOP 重要部分全解
如果你有耐心,那就请你慢慢的往下看,肯定有你用的到的地方,请你相信我! 现在你看到的只是其中一部分后面,还有,还没更新出来,待续.... 类对象的定义 类是现实世界或思维世界中的实体在计算机中的反映, ...
- 关于在工程中添加新文件时的LNK2019错误的一个解决办法
我这几天一直在研究Qt的串口程序,在读懂了官方给出的实例程序后我决定把其多线程的串口监视程序加入到我自己的工程中,便直接把问价复制到自己的工程下面,在Qt中加入到自己的工程中,但是总是出现LNK201 ...