码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-07-29 21:28:35      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

Language:
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 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.

题意:略..O(∩_∩)O哈哈~偷回懒~~~~

这是一道比较裸的多重背包可以直接套用多重背包的模板,但是这个题目只需要判断可行性,我们只需要二进制处理一下即可,背包有很多变式,我们不能每次都是套用模板,更要学会各种背包的问题的思路。比如多重背包,最核心的思想就是二进制优化了,用二进制优化,可以将复杂度O(VΣMi)降低到O(VΣlogMi),真是神奇!!!

那么,什么是多重背包?多重背包:有N种物品和一个容量为V的背包。第i种物品最多有Mi件可用,每件耗费的空间是Ci,价值是Wi。求解将哪O(VΣlogMi)些物品装入背包可使这些物品的耗费的空间总和不超过背包容量,且价值总和最大。

这里还使用到了滚动数组来优化空间复杂度。

最后,给大家提供两种结题

//方法一:Memory :252KB Time:16ms
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
const int maxn = 20000 + 5;
int num[7], sum;
bool suc, dp[6 * maxn];
void pack01(int C)
{
    for(int v = sum; v >= C; v--)
        dp[v] |= dp[v - C];
}
int main()
{
    int cas = 0;
    //freopen("input.in", "r", stdin);
    while(~scanf("%d", &num[1]))
    {
        sum = num[1];
        for(int i = 2; i <= 6; i++)
        {
            scanf("%d", &num[i]);
            sum += num[i] * i;
        }
        if(sum == 0) break;
        if(cas) puts("");
        printf("Collection #%d:\n", ++cas);
        if(sum & 1)
        {
            printf("Can't be divided.\n");
            continue;
        }
        suc = false;
        sum >>= 1;
        memset(dp, false, sizeof(dp));
        dp[0] = true;
        for(int i = 1; i <= 6; i++)
        {
            int k = 1;
            while(k < num[i])
            {
                pack01(k * i);
                num[i] -= k;
                k <<= 1;
            }
            pack01(num[i]*i);
        }
        printf("%s\n", dp[sum] ? "Can be divided." : "Can't be divided.");
    }
    return 0;
}
<pre name="code" class="cpp">//方法二:多重背包模板 Memory:620KB Time:16ms
#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
const int maxn = 20000+5;
int num[7],dp[maxn*6],V;
bool suc;
void Pack01(int C,int W)
{
    if(suc) return ;
    for(int v = V;v >= C;v--)
    {
        dp[v] = max(dp[v],dp[v-C]+W);
        if(dp[v] == V)
        {
            suc = true;
            return;
        }
    }
}
void ComplePack(int C,int W)
{
    if(suc) return;
    for(int v = C;v <= V;v++)
    {
        dp[v] = max(dp[v],dp[v-C]+W);
        if(dp[v] == V)
        {
            suc = true;
            return;
        }
    }
}
void MultiPack(int C,int W,int M)
{
    if(C*M>=V)
    {
        ComplePack(C,W);
        return;
    }
    int k = 1;
    while(k < M)
    {
        Pack01(k*C,k*W);
        if(suc) return;
        M -= k;
        k <<= 1;
    }
    Pack01(C*M,W*M);
}
int main()
{
    int cas = 0;
    //freopen("input.in","r",stdin);
    while(~scanf("%d",&num[1]))
    {
        int sum = num[1];
        for(int i = 2; i <= 6; i++)
        {
            scanf("%d",&num[i]);
            sum += num[i]*i;
        }
        if(sum == 0) break;
        if(cas) puts("");
        printf("Collection #%d:\n",++cas);
        if(sum & 1)
        {
            printf("Can't be divided.\n");
            continue;
        }
        suc = false;
        V = sum>>1;
        memset(dp,-1,sizeof(dp));
        dp[0] = 0;
        for (int i = 1;i <= 6;i++)
        {
            MultiPack(i,i,num[i]);
            if(suc) break;
        }
        printf("%s\n",suc?"Can be divided.":"Can't be divided.");
    }
    return 0;
}



</pre><pre>



    
        

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:

原文地址:http://blog.csdn.net/acmore_xiong/article/details/47131251

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!