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

HDU1059——多重部分和问题——Dividing

时间:2015-05-29 21:36:23      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

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.
 

 

Source
 

 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1114 1081 1074 1078 1160 
大意:有6种货币,每种货币不超过20000数目,价值从1到6,问你是否能到达一个数num,num=sum/2,sum指总的价值
多重部分和问题模型 (来自挑战程序设计竞赛):
有n种不同大小的数字ai,每种各mi个。判断是否可以从这些数字中选出若干使他们的和恰好为K
O(kΣimi)
定义:dp[i+1][j] 表示用前i种数字是否能加合成j
状态转移方程  dp[i+1][j] = (0<=k<=mi且k*a[i] <= j 时存在使dp[i][j-k*a[i]]为真的k)
即 dp[i+1][j] |= dp[i][j-k*a[i]);
int n,K;
int a[MAX_N];
int m[MAX_N];
bool dp[MAX_N+1][MAX_N+1];

void solve()
{
    dp[0][0] = true;
    for(int i = 0 ; i < n ;i++){
        for(int j = 0 ; j <= K; j++){
            for(int k = 0 ; j <= m[i] && k *a[i] <= j ;k++){
                dp[i+1][j] |= dp[i][j - k*a[i]];
            }
        }
    }
    if(dp[n][K]) printf("Yes\n");
    else pritnf("No\n");
}

上述复杂度过高

O(nk)

定义:dp[i+1][j] 表示用前i种数加和得到j时第i种数最多能剩余多少个(不能加和得到i的情况下为-1)

              m[i]   (dp[i][j] >= 0)

动态转移方程  dp[i+1][j] =     -1      (j < a[i] || dp[i+1][j-a[i]] <= 0)

              dp[i+1][j-a[i]] - 1  else 
初始化为-1
int n,K;
int a[MAX_N];
int m[MAX_N];
int dp[MAX_K+1];
void solve()
{
    memset(dp,-1,sizeof(dp));
        dp[0] = 0;
    for(int i = 0 ; i < n ;i++){
        for(int j = 0 ; j <= K; j++){
            if(dp[j] >= 0){
                dp[j] = m[i];
            }
            else if(j < a[i] || dp[j-a[i]] <= 0){
                dp[j] = -1;
            }
            else 
                dp[j] = dp[j-a[i]] - 1;
        }
    }
    if(dp[K] >= 0) printf("Yes\n");
    else printf("No\n");
}

AC代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[121000];
int main()
{
    int a[7] = {1,2,3,4,5,6};
    int m[7];
    int col = 1;
       while(~scanf("%d%d%d%d%d%d",&m[0],&m[1],&m[2],&m[3],&m[4],&m[5])){
        memset(dp,-1,sizeof(dp));
        if(m[0] + m[1] + m[2] + m[3] + m[4] + m[5] == 0)
        break;
        printf("Collection #%d:\n",col++);
        if((m[0] + m[1]*2 + m[2]*3 + m[3]*4 + m[4]*5 + m[5]*6)%2 == 1){
            printf("Can‘t be divided.\n");
        }
        else {
            int sum = (m[0] + m[1]*2 + m[2]*3 + m[3]*4 + m[4]*5 + m[5]*6)/2;
                dp[0] = 0;
                for(int i = 0; i < 6; i++){
                    for(int j = 0 ; j <= sum ;j++){
                        if(dp[j] >= 0) dp[j] = m[i];
                        else if(j < a[i] || dp[j-a[i]] <= 0){
                            dp[j] = -1;
                        }
                        else {
                            dp[j] = dp[j-a[i]] - 1;
                        }
                    }
                }
                if(dp[sum] >= 0)
                 printf("Can be divided.\n");
                else printf("Can‘t be divided.\n");
        }
         puts(""); 
    }
    return 0;
}

  

 

HDU1059——多重部分和问题——Dividing

标签:

原文地址:http://www.cnblogs.com/zero-begin/p/4539398.html

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