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为第几组测试用例

第二行:是否能分(具体形式见用例)

第三行:空白(必须注意,否则PE)

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

#include <cstdio>
#include <iostream>
#define INF 100000000
using namespace std;

int f[240005];  //f[j]相当于f[i][j]: 考虑1...i个物品,恰好放到容量为j,所能达到的最大价值
int v; //背包容量
int max(int a,int b)
{
	if(a > b)
		return a;
	return b;
}
//处理一个完全背包 (该种物品不限量)
void complete_pack(int *a, int c, int w)
{
    for(int i = c; i <= v; i++)
        a[i] = max(a[i], a[i - c] + w);
}  

//处理一个 01背包 (该种物品只有一个)
void zeroone_pack(int *a, int c, int w)
{
    for(int i = v; i >= c; i--)
        a[i] = max(a[i], a[i - c] + w);
}  

//处理一个多重背包 (该种物品指定上限)
void mutiple_pack(int *a, int c, int w, int m)
{
    //若该种物品足以塞满背包-->转化为完全背包
    if(c * m >= v)
	{
        complete_pack(a, c, w);
        return;
    }

    /*二进制思想拆分:多重背包中的一个物品--变成-->0-1背包中的多个物品
	容量:2^0    2^1    2^2    2^k    m-∑前面             ***保证k达到最大值
	价值:2^0*c  2^1*c  2^2*c  2^k*c  (m-∑前面 )*c
    */
    int k = 1;
    while(k <= m)
    {
        zeroone_pack(a, k * c, k * w);
        m = m - k;
        k = 2 * k;
    }
}  

int main()
{
    //freopen("d:/data.in","r",stdin);
    //freopen("d:/data.out","w",stdout);
    int sum, i, c[7], w[7], m[7],cas = 0;
    while(scanf("%d%d%d%d%d%d", &m[1], &m[2], &m[3], &m[4], &m[5], &m[6]))
	{
        if(m[1] == 0 && m[2] == 0 && m[3] == 0 && m[4] == 0 && m[5] == 0 && m[6] == 0)
			break;
        sum = 0;
        for(i = 1; i <= 6; i++)
		{
            c[i] = w[i] = i;
            sum += c[i] * m[i];
        }
        printf("Collection #%d:\n", ++cas);
        if(sum %2 == 1)
		{
            printf("Can't be divided.\n\n");
        }
		else
		{
            sum /= 2;
            v = sum;
            for(i = 1; i <= sum; i++)
				f[i] = -INF;
            f[0] = 0;  

            for(i = 1; i <= 6; i++)
				mutiple_pack(f, c[i], w[i], m[i]);
            if(f[v] != v)//其实只要f[v]有正值就可以
			{
                printf("Can't be divided.\n\n");
            }
			else
			{
                printf("Can be divided.\n\n");
            }
        }
    }
    return 0;
}

poj1014 Dividing 背包

时间: 2024-10-12 02:32:45

poj1014 Dividing 背包的相关文章

POJ 1014 Dividing 背包

这道题使用多重背包,不过其实我也不太明白为什么叫这个名字. 因为感觉不是什么多重,而是物体的分解问题. 就是比如一个物体有数量限制,比如是13,那么就需要把这个物体分解为1, 2, 4, 6 如果这个物体有数量为25,那么就分解为1, 2, 4, 8, 10 看出规律吗,就是分成2的倍数加上位数,比如6 = 13 - 1 - 2 - 4, 10 = 25 - 1 - 2 - 4 - 8,呵呵,为什么这么分解? 因为这样分解之后就可以组合成所有1到13的数,为25的时候可以组合成所有1到25的数啦

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

Dividing Time Limit: 1000MS Memory Limit: 10000K 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

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:Dividing(多重背包)

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. This would be easy if all the marbles had the same value, b

POJ1014 Dividing 母函数

Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59666   Accepted: 15417 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 marbl

poj分类解题报告索引

图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Journey poj1724 - ROADS(邻接表+DFS) BFS poj3278 - Catch That Cow(空间BFS) poj2251 - Dungeon Master(空间BFS) poj3414 - Pots poj1915 - Knight Moves poj3126 - Prim

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 多重背包,二进制物品拆分转01背包

直接做01背包,即把物品数量累加,做20000物品的01背包指定TLE,不用我说了吧! 本文的优化是二进制优化,O(logn),至于完全背包记录已使用个数的O(n)算法本文不进行讲解,在博客的"背包"分类里. 二进制优化: 大家知道一个十进制数可以转换成二进制,那么假设某种物品有1023种,即2^10-1,二进制为111111111,则可以视为每一位分别是一个由{1,2,4,8,16,32,64,128,256,512}个物品糅合成的大物品,二进制数每一位0表示不取,1表示取,这样我们

poj1014 hdu1059 Dividing 多重背包

有价值为1~6的宝物各num[i]个,求能否分成价值相等的两部分. #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #def