POJ 1014 / HDU 1059 Dividing 多重背包+二进制分解

Problem 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 describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, ..., 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 colletcion, 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.


题意:
给出6种大小的石头,问能不能分成数值一样的两堆。

思路:背包方面是简单的,但主要的问题是,如果一个一个枚举物品,物品太多肯定会超时。所以就有将物品的数量进行二进制分解,即分成2的幂次,每个2的幂都作为一个新的物品,再进行01背包就行了。POJ上交不但不支持<bits/stdc++.h>而且数组还要开大一点/.

/** @Date    : 2016-12-10-20.39
  * @Author  : Lweleth ([email protected])
  * @Link    : https://github.com/
  * @Version :
  */
#include
#include
#include
#include
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;

int cnt = 0;
int a[10];
int b[N];

int dp[60010];

void binaDiv(int x, int v)
{
    int r = 1;
    while(r < x)
    {
        b[cnt++] = r * v;
        x -= r;
        r <<= 1;
    }
    b[cnt++] = x * v;
}

int main()
{
    int c = 0;
    while(true)
    {
        int sum = 0;
        for(int i = 1; i <= 6; i++)
            scanf("%d", a + i), sum += a[i]*i;
        if(sum == 0)
            break;
        printf("Collection #%d:\n", ++c);
        if(sum & 1)
        {
            printf("Can‘t be divided.\n\n");
            continue;
        }
        sum /= 2;

        cnt = 0;
        for(int i = 1; i <= 6; i++)
        {
            binaDiv(a[i], i);
        }
        MMF(dp);
        for(int i = 0; i < cnt; i++)
        {
            for(int j = sum; j >= 0; j--)
            {
                if(j >= b[i])
                    dp[j] = max(dp[j], dp[j - b[i]] + b[i]);
            }
        }
        if(dp[sum] == sum)
            printf("Can be divided.\n\n");
        else printf("Can‘t be divided.\n\n");
    }
    return 0;
}
时间: 2024-10-19 08:26:15

POJ 1014 / HDU 1059 Dividing 多重背包+二进制分解的相关文章

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. 我能只要看看从所有

hdu(1059) Dividing(多重背包)

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

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(多重背包加二进制优化)

http://acm.hdu.edu.cn/showproblem.php?pid=1059 Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29901    Accepted Submission(s): 8501 Problem Description Marsha and Bill own a collection

【背包专题】G - Dividing hdu 1059【多重背包】

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 colle

hdu 1059 Dividing(完全背包)

/* *********************************************** Author :xryz Email :[email protected] Created Time :2015/7/16 8:29:33 File Name :C:\Users\Administrator\Desktop\0001.cpp ************************************************ */ #include <stdio.h> #inclu

HDU 1059 Dividing dp背包题解

背包问题,这一类问题应用很广了. 本题可以根据特例优化一下. #include <stdio.h> #include <vector> #include <string.h> #include <algorithm> #include <iostream> #include <string> #include <limits.h> #include <stack> #include <queue>

HDU 1059 多重背包+二进制优化

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

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