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. AngularJS 学习笔记--01

    学习 AngularJS 要先了解 MVC 模式 , 即 " 模型--视图--控制器 " . 模型: 包含了需要用到的数据 ; 有两种广义上的模型 : 视图模型 , 只表示从控制器 ...

  2. mysql(一) 关联查询的方式

    mysql做关联查询时,一般使用join....on.....的语法. 但还有其它两种语法形式,三者的主要区别在于书写形式,其余方面并无太多差异. 如下三种形式: select * from trad ...

  3. BZOJ4860 Beijing2017树的难题(点分治+单调队列)

    考虑点分治.对子树按照根部颜色排序,每次处理一种颜色的子树,对同色和不同色两种情况分别做一遍即可,单调队列优化.但是注意到这里每次使用单调队列的复杂度是O(之前的子树最大深度+该子树深度),一不小心就 ...

  4. BZOJ3631:[JLOI2014]松鼠的新家——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=3631 https://www.luogu.org/problemnew/show/P3258 松鼠的 ...

  5. HDU5726:GCD——题解

    题目:hdu的5726 (我原博客的东西,正好整理过来,属于st表裸题) (可以看出我当时有多么的菜--) 这道题写了一遍,然而蒟蒻的我的时间爆炸了-- 于是看了一下学长的代码(顺便在此处%一下学长) ...

  6. Manacher以及回文树算法学习

    Manacher以及回文树算法学习 一.Manacher 关于\(Manacher\),这篇博客 讲的很清楚. 大致总结一下 为了将长度为奇数的回文串和长度为偶数的回文串一起考虑,需要在原字符串中插入 ...

  7. springmvc不通过controller进行页面跳转

    1.controller 继承WebMvcConfigureAdapter 然后使用ViewControllerRegistry  来进行跳转

  8. OpecnCV训练分类器详细整理

    本文主要是对下面网址博客中内容的实例操作: http://blog.csdn.net/byxdaz/article/details/4907211 在上述博客中,详细的讲述了OpenCV训练分类器的做 ...

  9. Qt ---------- connect连接类型

    Qt::AutoConnection 0 (Default) If the receiver lives in the thread that emits the signal, Qt::Direct ...

  10. 实现自己的Promise polyfill

    功能清单: Promise.prototype.then() Promise.prototype.catch() Promise.reject() Promise.resolve() Promise. ...