Dividing
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 65044   Accepted: 16884

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

 
题目倒是不难,挺久没认真搞算法了,多重背包写了半小时。。。贴个代码备忘
/*
ID: LinKArftc
PROG: 1014.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ;
int dp[maxn];
int cnt[];
int n, m; void pack01(int c, int v) {
for (int i = m; i >= c; i --) {
if (dp[i] < dp[i-c] + v) dp[i] = dp[i-c] + v;
}
} void packall(int c, int v) {
for (int i = c; i <= m; i ++) {
if (dp[i] < dp[i-c] + v) dp[i] = dp[i-c] + v;
}
} void packmult(int c, int v, int n) {
if (c * n >= m) {
packall(c, v);
return ;
}
int k = ;
while (k <= n) {
pack01(k * c, k * v);
n = n - k;
k *= ;
}
pack01(n * c, n * v);
} int main() {
//input;
int _t = ;
int tot;
while (~scanf("%d %d %d %d %d %d", &cnt[], &cnt[], &cnt[], &cnt[], &cnt[], &cnt[])) {
if (cnt[] == && cnt[] == && cnt[] == && cnt[] == && cnt[] == && cnt[] == ) break;
memset(dp, , sizeof(dp));
printf("Collection #%d:\n", _t ++);
tot = cnt[] * + cnt[] * + cnt[] * + cnt[] * + cnt[] * + cnt[] * ;
if (tot % ) {
printf("Can't be divided.\n\n");
continue;
}
m = tot / ;
for (int i = ; i < ; i ++) {
if (cnt[i]) packmult(i + , i + , cnt[i]);
}
if (m == dp[m]) printf("Can be divided.\n\n");
else printf("Can't be divided.\n\n");
} return ;
}

POJ1014(多重背包)的更多相关文章

  1. poj1014 dp 多重背包

    //Accepted 624 KB 16 ms //dp 背包 多重背包 #include <cstdio> #include <cstring> #include <i ...

  2. poj1014 Dividing (多重背包)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=1014">http://poj.org/problem?id=1014 Descrip ...

  3. poj1014二进制优化多重背包

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53029   Accepted: 13506 Descri ...

  4. hdu1059&poj1014 Dividing (dp,多重背包的二分优化)

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

  5. 洛谷P1782 旅行商的背包[多重背包]

    题目描述 小S坚信任何问题都可以在多项式时间内解决,于是他准备亲自去当一回旅行商.在出发之前,他购进了一些物品.这些物品共有n种,第i种体积为Vi,价值为Wi,共有Di件.他的背包体积是C.怎样装才能 ...

  6. HDU 2082 找单词 (多重背包)

    题意:假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26.那么,对于给定的字母,可以找到多少价值<=50的 ...

  7. Poj 1276 Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26172   Accepted: 9238 Des ...

  8. poj 1276 Cash Machine(多重背包)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33444   Accepted: 12106 De ...

  9. (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)

    http://poj.org/problem?id=3260   Description Farmer John has gone to town to buy some farm supplies. ...

随机推荐

  1. 修改MSSQL字段类型

    update Data_Project set SyncTime=NULL; alter table Data_Project alter column SyncTime datetime; upda ...

  2. 【ASP.NET Core】ASP.NET Core API 版本控制

    几天前,我和我的朋友们使用 ASP.NET Core 开发了一个API ,使用的是GET方式,将一些数据返回到客户端 APP.我们在前端进行了分页,意味着我们将所有数据发送给客户端,然后进行一些dat ...

  3. Swift学习与复习

    swift中文网 http://www.swiftv.cn http://swifter.tips/ http://objccn.io/ http://www.swiftmi.com/code4swi ...

  4. Django 2.0 学习(10):Django 定制化

    定制化admin表单 通过使用admin.site.register(Question)注册Question模型,Django可以构造默认的表单.通常,可以通过对象的注册机制来告诉Django我们想要 ...

  5. 【以前的空间】poj 2288 Islands and Bridges

    一个不错的题解 : http://blog.csdn.net/accry/article/details/6607703 这是一道状态压缩.每个点有一个值,我们最后要求一个最值sum.sum由三部分组 ...

  6. bzoj 3132: 上帝造题的七分钟 (二维树状数组)

    推推公式,最后变成四个东西的前缀和 然后不知道为什么一直wa,数据在本地测是没有错的& 好心的管理员还给了某位p党大神a了的代码,感人肺腑(虽然还是没发现到底我的程序是问题) var f1,f ...

  7. 项目管理---git----快速使用git笔记(二)------git的本地安装

    下载安装包 在使用Git前我们需要先安装 Git.Git 目前支持 Linux/Unix.Solaris.Mac和 Windows 平台上运行. Git 各平台安装包下载地址为:http://git- ...

  8. 搞笑的代码 ( funny )

    搞笑的代码 ( funny ) 在OI界存在着一位传奇选手——QQ,他总是以风格迥异的搞笑代码受世人围观 某次某道题目的输入是一个排列,他使用了以下伪代码来生成数据 while 序列长度<n d ...

  9. 【单调队列】【P2627】 修剪草坪

    传送门 Wa这次竟然不是Uva的题 Description 在一年前赢得了小镇的最佳草坪比赛后,Farm John变得很懒,再也没有修剪过草坪.现在,新一轮的最佳草坪比赛又开始了,Farm John希 ...

  10. 1143: [CTSC2008]祭祀river(最长反链)

    1143: [CTSC2008]祭祀river 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1143 Description: 在遥远的 ...