HDU-1059 Dividing(DP)

77 篇文章 0 订阅

Dividing
http://acm.hdu.edu.cn/showproblem.php?pid=1059

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.

 

初学DP,只知道01背包,没看过多重背包,只是感觉应该这样做,超时n次,网上看到别人的解法和分析证明收获颇多

http://www.cnblogs.com/walker01/archive/2010/02/06/1665033.html这个解法还是看不懂,待以后懂的更多了再来看

#include <cstdio>
#include <cstring>
#include <algorithm>
#define LOCAL
using namespace std;

const int MAXN=60005;
int m,maxv,n[9];
int dp[MAXN];
int mod[7]={1,42,42,14,84,210,42};

int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif // LOCAL
    int kase=0;
    while(scanf("%d%d%d%d%d%d",&n[1],&n[2],&n[3],&n[4],&n[5],&n[6]),(n[1]||n[2]||n[3]||n[4]||n[5]||n[6])) {
        printf("Collection #%d:\n",++kase);
        //优化法一:			http://poj.org/bbs?problem_id=1014
        //POJ上给出的一种优化,不明白为什么
        /*
        for(int i=1;i<=6;++i)
            if(n[i]>60) {
                if(1==(1&n[i]))
                    n[i]=61;
                else
                    n[i]=60;
            }
        */

        //优化法二:自己苦想半天想出来的取模优化,
        //          看了			http://blog.csdn.net/gg_gogoing/article/details/38453273
        //          才知道取模原来都是错的...
        //          同时又看到:取模前不为0,取模结果为0时,重新赋为模可避免问题(不知道能不能避免全部错误的发生)

        //1+2+3+4+5+6=21,则对i和21的最小偶公倍数取模,刚好能消去21*2n(n=0,1,2……),每一堆都能分得21*n
        for(int i=1;i<=6;++i) {
            bool flag=true;
            if(0==n[i])
                flag=false;
            n[i]%=mod[i];
            if(0==n[i]&&flag)
                n[i]=mod[i];
        }
        int mini=n[1];
        for(int i=2;i<=6;++i)
            if(n[i]<mini)
                mini=n[i];
        if(1==(mini&1))
            --mini;
        for(int i=1;i<=6;++i)
            n[i]-=mini;

        maxv=0;
        for(int i=1;i<=6;++i)
            maxv+=n[i]*i;
        if(1==(maxv&1)) {
            printf("Can't be divided.\n\n");
            continue;
        }
        m=maxv/2;
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=6;--n[i]) {
            if(n[i]==0) {
                ++i;
                ++n[i];
                continue;
            }
            for(int j=m;j>=i;--j) {
                dp[j]=max(dp[j-i]+i,dp[j]);
            }
        }
        if(m!=dp[m])
            printf("Can't be divided.\n\n");
        else
            printf("Can be divided.\n\n");
    }
    return 0;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值