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, 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.

大致题意:

有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,使两份的总价值相等,其中一个物品不能切开,只能分给其中的某一方,当输入六个0是(即没有物品了),这程序结束,总物品的总个数不超过20000

输出:每个测试用例占三行:

第一行: Collection #k: k为第几组测试用例

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

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

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int dp[1010000],w[101],v[101];
int V,K=0;
void wpack(int w)
{
    for(int i=w; i<=V; i++)
    {
        if(dp[i-w]+w>dp[i])
            dp[i]=dp[i-w]+w;
    }
}
void pack1(int w)
{
    for(int i=V; i>=w; i--)
    {
        if(dp[i-w]+w>dp[i])
            dp[i]=dp[i-w]+w;
    }
}
void Mul(int w,int num)
{
    if(w*num>=V)
    {
        wpack(w);
        return ;
    }
    int k=1;
    while(k<num)
    {
        pack1(k*w);
        num-=k;
        k=k*2;
    }
    pack1(num*w);
}
int main()
{
    while(scanf("%d%d%d%d%d%d",&w[1],&w[2],&w[3],&w[4],&w[5],&w[6])!=EOF)
    {
        K++;
        V=w[1]+w[2]+w[3]+w[4]+w[5]+w[6];
        if(V==0) break;
        V=w[1]*1+w[2]*2+w[3]*3+w[4]*4+w[5]*5+w[6]*6;
        if(V%2==1)
        {
            printf("Collection #%d:\n",K);
            printf("Can‘t be divided.\n\n");
        }
        else
        {
            V=V/2;
            memset(dp,0,sizeof(dp));
            for(int i=1; i<=6; i++)
            {
                Mul(i,w[i]);
            }
            if(dp[V]==V)
            {
                printf("Collection #%d:\n",K);
                printf("Can be divided.\n\n");
            }
            else
            {
                printf("Collection #%d:\n",K);
                printf("Can‘t be divided.\n\n");
            }
        }
    }
    return 0;
}
时间: 2024-10-14 06:30:08

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

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

HDU 1059 Dividing(多重背包)

HDU 1059 Dividing(多重背包) http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意: 现在有价值为1,2,3,4,5,6的6种物品, 它们的数量为num[i]( 1<=i<=6 )个. 现在要问的是能否把所有的的物品分成两份且这两份物品的价值总和相同 ? 分析: 首先我们求出所有物品的价值和sum_val, 如果sum_val是奇数, 那么明显不能分. 那么sum_val为偶时, 我们令m=sum_val/2. 我能只要看看从所有

POJ 1014 Dividing (多重背包)

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

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 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 marbl

hdu 1059 Dividing(多重背包优化)

Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 20635    Accepted Submission(s): 5813 Problem Description Marsha and Bill own a collection of marbles. They want to split the collection

hdu(1059) Dividing(多重背包)

题意:输入六个数,价值分别为1——6,输入的数代表该价值的物品的个数:求能否平均分. key:如果奇数肯定不能分,直接输出答案.偶数的话,就是多重背包问题. 试过两种做法,第一种是背包九讲的二进制优化,写三个函数,分别是bag01, bagall, bagmulti~第二种是直接多重背包,但很可能tle,这题我交的就是tle了~ #include <iostream> #include <algorithm> #include <string.h> #include &

【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

台州 OJ 1315 Dividing 多重背包

题目的大概意思就是,有 6 种石头,价值分别是 1,2,3,4,5,6,给出他们的数量,求是否能将他们平分成两组价值相同的石头. 设石头的总价值为sum.把石头的价值看成重量,则问题转换成是否能恰好装下指定重量的石头,及背包容量为 sum/2 时,是否存在恰好装下一些石头的情况. 代码: #include <iostream> #include <cstring> using namespace std; const int MAX = 120005; int dp[MAX]; i