ACM学习历程—HDU 1059 Dividing(dp && 多重背包)

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.

题目大意就是判断这么多数字能不能均分成两类,每个数字不可拆分。

看完题目就感觉是个多重背包。不过这里只用判断sum/2能否被装到。于是就不用判断sum/2+1到sum的背包了。

由于只用判断是否能装到,于是只用开bool型数组即可。

由于每个数字有一定的使用次数,所以需要开vis数组,而且对于每一种数字的背包需要初始化全为1。

由于考虑到需要最优解,所以对于dp[j]为真的情况,就不需要再判断dp[j-i]了,因为如果再靠dp[j-i]来放下num[i]的话,就浪费了一次数字i的使用。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define N 1000000007

using namespace std;

int num[7], sum;
int vis[60005];
bool dp[60005];

bool Input()
{
    sum = 0;
    memset(dp, 0, sizeof(dp));
    for (int i = 1; i <= 6; ++i)
    {
        scanf("%d", &num[i]);
        sum += i*num[i];
    }
    if (sum)
        return true;
    else
        return false;
}

void Work()
{
    if (sum % 2)
    {
        printf("Can‘t be divided.\n\n");
        return;
    }
    sum /= 2;
    dp[0] = true;
    for (int i = 1; i <= 6; ++i)
    {
        if (num[i] == 0)
            continue;
        memset(vis, 0, sizeof(vis));
        for (int j = i; j <= sum; j++)
        {
            if (dp[j-i] && !dp[j] && vis[j-i] < num[i])
            {
                dp[j] = true;
                vis[j] = vis[j-i]+1;
            }
        }
    }
    if (dp[sum])
        printf("Can be divided.\n\n");
    else
        printf("Can‘t be divided.\n\n");
}

int main()
{
    //freopen("test.in", "r", stdin);
    int times = 1;
    while (Input())
    {
        printf("Collection #%d:\n", times);
        Work();
        times++;
    }
    return 0;
}
时间: 2024-11-06 15:55:08

ACM学习历程—HDU 1059 Dividing(dp && 多重背包)的相关文章

HDU 1059 Dividing (多重背包)

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

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

hdoj 1059 Dividing 【多重背包】||【优化母函数】

题意:每一种弹珠(marble)的都有各自的价值,第一种为1, 第二种为2,..,给出你每种弹珠的数量,求能不能将价值总和均分. 策略:rt: 这道题比赛的时候没有想到用母函数,就用了多重背包来解,之后递交的时候时间居然超600ms,我再一看递交排行都是0ms(⊙﹏⊙b汗).看到讨论区有人说母函数也可以,于是就写了个普通的,可惜TL了.果然还是需要优化啊...于是游来游去果然0ms(O(∩_∩)O哈哈哈~). 说一下0秒的思路,因为只是问能不能均分,所以我们没有必要找种类数,找到了就定义为1好了

hdu 1059 Dividing DP,多重背包 测试数据很水

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

ACM学习历程—HDU 3915 Game(Nim博弈 &amp;&amp; xor高斯消元)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所有xor和为0. 那么自然变成了n个数里面取出一些数,使得xor和为0,求取法数. 首先由xor高斯消元得到一组向量基,但是这些向量基是无法表示0的. 所以要表示0,必须有若干0来表示,所以n-row就是消元结束后0的个数,那么2^(n-row)就是能组成0的种数. 对n==row特判一下. 代码:

ACM学习历程—HDU 5534 Partial Tree(动态规划)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x1)+f(x2)+...+f(xn)的最大值. 首先由于是树,所以有n-1条边,然后每条边连接两个节点,所以总的度数应该为2(n-1). 此外每个结点至少应该有一个度. 所以r1+r2+...rn = 2n-2.ri >= 1; 首先想到让ri >= 1这个条件消失: 令xi = ri,则x1+x

ACM学习历程—HDU 5536 Chip Factory(xor &amp;&amp; 字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. 此外题目中说大数据只有10组,小数据最多n只有100.(那么c*n^2的复杂度应该差不多) 于是可以考虑枚举i和j,然后匹配k. 于是可以先把所有s[k]全部存进一个字典树, 然后枚举s[i]和s[j],由于i.j.k互不相等,于是先从字典树里面删掉s[i]和s[j],然后对s[i]+s[j]这个

ACM学习历程—HDU 3949 XOR(xor高斯消元)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的值都求出来,对于这个规模的n是不可行的. 然后之前有过类似的题,求最大的,有一种方法用到了线性基. 那么线性基能不能表示第k大的呢? 显然,因为线性基可以不重复的表示所有结果.它和原数组是等价的. 对于一个满秩矩阵 100000 010000 001000 000100 000010 000001