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

题目大意:

有六个大理石,他们的价值分别是1,2,3,4,5,6,然后分别给出六个大理石的个数,问如何平分给两个人,令两个人所得到的价值相等。

这里要求恰好达到平分这个状态,也就是当背包大小是价值的一半恰好装满,所以dp的初始值是负无穷。

开始以为数据小(没看清题),其实大的该死,我就用01背包去做,果断超时。

所以就改成多重背包改二进制优化的01背包+完全背包。就是个模板了。好像只要优化的01背包也能过,不过不知道怎么做。

下面是模拟多重背包问题,总共6个物品,将第i个物品的价值与费用都看做是i,这样套用背包九讲的多重背包模板就可以了,不过注意的是数组要开的大一点.

代码:16MS (这是codeblocks运行的代码)

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define M 100005
#define INF -99999;
int sum,v[7],dp[M];
void CompletePack(int cost,int wight)
{
    int i;
    for(i=cost;i<=sum;i++)
        dp[i]=max(dp[i],dp[i-cost]+wight);
}
void ZeroOnePack(int cost,int wight)
{
    int i;
    for(i=sum;i>=cost;i--)
        dp[i]=max(dp[i],dp[i-cost]+wight);
}
void MultiplePack(int cost,int wight,int amount)
{
    if(cost*amount>=sum)
    {
        CompletePack(cost,wight);
        return;
    }
    int k=1;
    while(k<amount)
    {
        ZeroOnePack(k*cost,k*wight);
        amount-=k;
        k=k<<1;
    }
    ZeroOnePack(amount*cost,amount*wight);
}
void DP()
{
    int i,j,k;
    for(i=0;i<=sum;i++) dp[i]=i==0?0:INF;
    for(i=1;i<=6;i++)
        MultiplePack(i,i,v[i]);
}
int main()
{
    int flag,i,t=0;
    while(1)
    {   t++;sum=0;
        for(i=1;i<=6;i++)
    {
        cin>>v[i];
        sum+=i*v[i];
    }
    if(!sum) break;
    printf("Collection #%d:\n",t);
        if(sum&1){
            printf("Can‘t be divided.\n\n");
            continue;
        }
        sum=sum>>1;
        DP();
        if(dp[sum]>=0)
            printf("Can be divided.\n\n");
        else
            printf("Can‘t be divided.\n\n");
    }
    return 0;
}

POJ 1014 Dividing (多重背包)

时间: 2024-08-18 22:05:22

POJ 1014 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的数啦

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 1742 Coins (多重背包)

Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 28448   Accepted: 9645 Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some

POJ 1742 Coins 多重背包单调队列优化

http://poj.org/problem?id=1742 题意: 很多硬币,有价值和数量,给出一个上限,问上限内有多少种钱数可以由这些硬币组成. 分析: 好像是楼教主男人八题之一.然后学多重背包单调队列优化时看了别人的程序..所以后来写了就1A了=.= 前一篇小小总结了一下多重背包单调队列优化(http://www.cnblogs.com/james47/p/3894772.html),这里就不写了. 1 #include<cstdio> 2 #include<cstring>

POJ 1014 Dividing(多重背包+二进制优化)

http://poj.org/problem?id=1014 题意:6个物品,每个物品都有其价值和数量,判断是否能价值平分. 思路: 多重背包.利用二进制来转化成0-1背包求解. 1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 #include<algorithm> 6 using namespace std; 7 8 const i

POJ 1014 Dividing 【DP 之 多重背包 / 二进制优化】

Language: Default Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63647   Accepted: 16488 Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal

POJ 1014 Dividing(多重背包)

Dividing 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

POJ 1014 Dividing【多重背包+二进制优化】

大意: 价值1, 2, 3, ……, 6的物品分别a1, a2, ……, a5, a6件 问能否把这些物品分成两份,使其具有相同的价值(所有物品必须全部用上) 分析: 给个物品有多件,即多重背包 只要看能不能将这些物品拼成   总价值 的 一半就可以了 转化为01背包是用二进制优化,否则会超时 代码: 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std;

POJ 1837 Balance (多重背包计数)

Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11706   Accepted: 7305 Description Gigel has a strange "balance" and he wants to poise it. Actually, the device is different from any other ordinary balance. It orders two arms