Dividing
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 68769   Accepted: 17955

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.

Source


裸题
回顾一下两种做法
然而O(nv)反而被二进制拆分虐了
//
// main.cpp
// poj1014
//
// Created by Candy on 9/21/16.
// Copyright © 2016 Candy. All rights reserved.
// #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=2e4*+;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n[],sum=,cnt=;
int f[N];
inline void zp(int v){
for(int i=sum;i>=v;i--) f[i]|=f[i-v];
}
inline void cp(int v){
for(int i=v;i<=sum;i++) f[i]|=f[i-v];
}
void mp(int v,int c){
if(c*v>sum) {cp(v);return;}
int k=;
while(k<c){
zp(k*v);
c-=k;
k*=;
}
zp(c*v);
}
int main(int argc, const char * argv[]) {
while(true){
++cnt; sum=; memset(f,,sizeof(f)); f[]=;
int flag=;
for(int i=;i<=;i++) {n[i]=read();sum+=i*n[i];if(n[i]) flag=;}
if(flag) break;
if(sum%) {
printf("Collection #%d:\nCan't be divided.\n\n",cnt);
continue;
}
sum/=;
for(int i=;i<=;i++) mp(i,n[i]);
if(!f[sum]) printf("Collection #%d:\nCan't be divided.\n\n",cnt);
else printf("Collection #%d:\nCan be divided.\n\n",cnt);
} return ;
}
//
// main.cpp
// poj1014
//
// Created by Candy on 9/21/16.
// Copyright © 2016 Candy. All rights reserved.
// #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=2e4*+;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n[],v[],sum=,cnt=;
int f[][N];
void mpAble(){
memset(f,-,sizeof(f));
f[][]=;
for(int i=;i<=;i++){
for(int j=;j<=sum;j++){
if(f[i-][j]>=) f[i][j]=n[i];
else f[i][j]=-;
}
for(int j=v[i];j<=sum;j++)
if(f[i][j-v[i]]>) f[i][j]=max(f[i][j],f[i][j-v[i]]-);
}
}
int main(int argc, const char * argv[]) {
while(true){
++cnt; sum=; memset(f,,sizeof(f));
int flag=;
for(int i=;i<=;i++){
n[i]=read();v[i]=i;sum+=i*n[i];
if(n[i]) flag=;
}
if(flag) break;
if(sum%) {
printf("Collection #%d:\nCan't be divided.\n\n",cnt);
continue;
}
sum/=;
mpAble();
if(f[][sum]==-) printf("Collection #%d:\nCan't be divided.\n\n",cnt);
else printf("Collection #%d:\nCan be divided.\n\n",cnt);
} return ;
}

POJ1014Dividing[多重背包可行性]的更多相关文章

  1. POJ1742 Coins[多重背包可行性]

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 34814   Accepted: 11828 Descripti ...

  2. POJ1276Cash Machine[多重背包可行性]

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32971   Accepted: 11950 De ...

  3. 【DP|多重背包可行性】POJ-1014 Dividing

    Dividing Time Limit: 1000MS Memory Limit: 10000K Description Marsha and Bill own a collection of mar ...

  4. poj1742硬币——多重背包可行性

    题目:http://poj.org/problem?id=1742 贪心地想,1.如果一种面值已经可以被组成,则不再对它更新: 2.对于同一种面值的硬币,尽量用较少硬币(一个)更新,使后面可以用更多此 ...

  5. POJ 1742 Coins 【多重背包DP】

    题意:有n种面额的硬币.面额.个数分别为A_i.C_i,求最多能搭配出几种不超过m的金额? 思路:dp[j]就是总数为j的价值是否已经有了这种方法,如果现在没有,那么我们就一个个硬币去尝试直到有,这种 ...

  6. poj1742 多重背包的可行性问题

    http://poj.org/problem? id=1742 Description People in Silverland use coins.They have coins of value ...

  7. 背包问题(01背包,完全背包,多重背包(朴素算法&&二进制优化))

    写在前面:我是一只蒟蒻~~~ 今天我们要讲讲动态规划中~~最最最最最~~~~简单~~的背包问题 1. 首先,我们先介绍一下  01背包 大家先看一下这道01背包的问题  题目  有m件物品和一个容量为 ...

  8. BZOJ.3425.[POI2013]Polarization(DP 多重背包 二进制优化)

    BZOJ 洛谷 最小可到达点对数自然是把一条路径上的边不断反向,也就是黑白染色后都由黑点指向白点.这样答案就是\(n-1\). 最大可到达点对数,容易想到找一个点\(a\),然后将其子树分为两部分\( ...

  9. $POJ1742\ Coins$ 多重背包+贪心

    Vjudge传送门 $Sol$ 首先发现这是一个多重背包,所以可以用多重背包的一般解法(直接拆分法,二进制拆分法...) 但事实是会TLE,只能另寻出路 本题仅关注“可行性”(面值能否拼成)而不是“最 ...

随机推荐

  1. [html5+java]文件异步读取及上传核心代码

    html5+java 文件异步读取及上传关键代码段 功能: 1.多文件文件拖拽上传,file input 多文件选择 2.html5 File Api 异步FormData,blob上传,图片显示 3 ...

  2. Spring(3)—— Junit框架单元测试

    Junit主要用于单元测试,即白盒测试.它是一个开源的由JAVA开发的一个用于测试的框架. Junit的几个基本概念:TestCase,TestSuite,TestFixtrue TestCase:代 ...

  3. SharePoint 2013 新建项目字段自动加载上次保存值

    1.点击进入NewForm.aspx页面,编辑页面,插入Script Editor WebPart,如下图: 2.插入后如下图,拖动AutoRecord WebPart到脚本编辑器上面,防止因为加载顺 ...

  4. .frame类库简单介绍与使用

    .a静态库是Unix推出的,几乎所有平台都可以使用: .framework是Apple推出的,是对.a静态库的封装,方便使用. 1.新建framework类库项目 2.生成的类库中,自定义的源头文件需 ...

  5. 生成iOSAPP的二维码

    1.打开iTunes,在"应用"里面搜索要找的APP 2.右键要生成二维码的APP,选择"拷贝链接" 3.百度一个二维码生成器 4.把刚才拷贝的链接粘贴进去,点 ...

  6. iOS开发--Swift 基于AFNetworking 3.0的网络请求封装

    Swift和OC基于AFNetworking的网络请求流程相同, 就是语法不同, 对于Swift语法不是很清楚的同学, 建议多看看API文档, 自己多多尝试. 写过OC的应该都明白每句话做什么的, 就 ...

  7. 设计模式 之 策略(Strategy)模式

    最近看了<head first 设计模式>一书,便总结了里面的一些内容,今天就简单介绍一下策略模式. 策略模式:定义了算法族,分别封装起来,让他们能够相互替换,此模式让算法的变化独立于使用 ...

  8. Linux Shell 网络层监控脚本(监控包括:连接数、句柄数及根据监控反馈结果分析)

    脚本监控: 获取最大句柄数的进程: 链接分析: 脚本片段: case "$handle" in 2) echo "The handle of the process : ...

  9. php设计模式 数据对象映射模式

    数据对象映射模式,是将对象和数据存储映射起来,对一个对象的操作会映射为对数据存储的操作. 在代码中实现数据对象映射模式,实现一个ORM类,将复杂的sql语句映射成对象属性的操作.对象关系映射(Obje ...

  10. MVC WebAPI 三层分布式框架开发

    版权声明:本文为博主原创文章,未经博主允许不得转载. 前言:SOA(面向服务的架构)是目前企业应用开发过程中普遍采用的技术,基于MVC WebAPI三层分布式框架开发,以此适用于企业信息系统的业务处理 ...