POJ1014(多重背包)

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

Mid-Central European Regional Contest 1999

题目倒是不难,挺久没认真搞算法了,多重背包写了半小时。。。贴个代码备忘

/*
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 = 200000;
int dp[maxn];
int cnt[10];
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 = 1;
    while (k <= n) {
        pack01(k * c, k * v);
        n = n - k;
        k *= 2;
    }
    pack01(n * c, n * v);
}

int main() {
    //input;
    int _t = 1;
    int tot;
    while (~scanf("%d %d %d %d %d %d", &cnt[0], &cnt[1], &cnt[2], &cnt[3], &cnt[4], &cnt[5])) {
        if (cnt[0] == 0 && cnt[1] == 0 && cnt[2] == 0 && cnt[3] == 0 && cnt[4] == 0 && cnt[5] == 0) break;
        memset(dp, 0, sizeof(dp));
        printf("Collection #%d:\n", _t ++);
        tot = cnt[0] * 1 + cnt[1] * 2 + cnt[2] * 3 + cnt[3] * 4 + cnt[4] * 5 + cnt[5] * 6;
        if (tot % 2) {
            printf("Can‘t be divided.\n\n");
            continue;
        }
        m = tot / 2;
        for (int i = 0; i < 6; i ++) {
            if (cnt[i]) packmult(i + 1, i + 1, cnt[i]);
        }
        if (m == dp[m]) printf("Can be divided.\n\n");
        else printf("Can‘t be divided.\n\n");
    }

    return 0;
}
时间: 2024-10-04 09:02:39

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

poj1014 dp 多重背包

1 //Accepted 624 KB 16 ms 2 //dp 背包 多重背包 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 using namespace std; 7 const int imax_n = 120005; 8 int f[imax_n]; 9 int amount[7]; 10 int v; 11 int n=6; 12 int max(int a,int b) 1

poj1014+hdu1059--A - Dividing(多重背包,二进制优化)

A - Dividing Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status 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

poj1014 Dividing (多重背包)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=1014">http://poj.org/problem?id=1014 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. Th

多重背包+二进制拆分 POJ1014

题意:有权值分别为1,2,3,4,5,6的大理石,每种都有若干块,能否把它们分成权值相等的2份.大理石的总数量不超过20000.(多重背包) 分析:判断dp[ V/2 ] ==V/2 即可,但过程如果用普通做法会超时,即多重背包当成01背包做效率很低,这时候要用二进制拆分优化,将复杂度变为  二进制拆分原理: 这里是指一个大数11101111 ,只要每一位上的1我们都有一个数,就可以表示出来这个大数   也就是用1 2 4 8 (1 10 100 1000..)  可以表示出任意的数 ,那么任意

poj1014 Dividing 背包

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://poj.org/problem?id=1014 大致题意: 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两份的总价值相等,其中一个物品不能切开,只能分给其中的某一方,当输入六个0是(即没有物品了),这程序结束,总物品的总个数不超过20000 输出:每个测试用例占三行: 第一行: Collection #k: k为第几组测试

【POJ3260】The Fewest Coins 多重背包+完全背包

A来B处买东西,价值M元,有N种钱,每种钱A有一定数量,而B有无限数量. 求最少用多少张钞票可以满足交易,比如样例,A出50+25,B找5,即可满足,需要3张. A用多重背包转移状态,B用完全背包. 本文的多重背包优化用的是O(n)算法,二进制转换的O(nlogn)实在懒得写了. 那种可以看http://blog.csdn.net/vmurder/article/details/39472419 " [POJ1014]Dividing 多重背包,二进制物品拆分转01背包 " 贴代码:

[hdu5445 Food Problem]多重背包

题意:一堆食物,有价值.空间.数量三种属性,一些卡车,有空间,价格,数量三种属性.求最少的钱(不超过50000)买卡车装下价值大于等于给定价值的食物,食物可以拆开来放. 思路:这题的关键是给定的条件:食物可以拆开来放.这个条件使得卡车和食物可以分开考虑,然后通过空间这个属性联系在一起.做两遍多重背包即可. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

hdu 2079 选课时间(题目已修改,注意读题) 多重背包

选课时间(题目已修改,注意读题) Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3162    Accepted Submission(s): 2472 Problem Description 又到了选课的时间了,xhd看着选课表发呆,为了想让下一学期好过点,他想知道学n个学分共有多少组合.你来帮帮他吧.(xhd认为一样学分的课没区别

HDU-1171 Big Event in HDU (多重背包)

Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002.The splitting is absolutely a big