zoukankan      html  css  js  c++  java
  • POJ Dividing (多重背包 || DFS)

    Dividing

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
    Total Submission(s) : 1   Accepted Submission(s) : 1
    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 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.
     
    Source
    PKU
     
     
    多重背包:
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    int num[7],V,flag;
    int dp[100010];
    
    void ZeroOnePack(int c,int w){
        for(int i=V;i>=c;i--){
            if(dp[i]<dp[i-c]+w)
                dp[i]=dp[i-c]+w;
            if(dp[i]==V){   //剪枝,当能够平分SumValue时退出 
                flag=1;
                return ;
            }
        }
    }
    
    void CompletePack(int c,int w){
        for(int i=c;i<=V;i++){
            if(dp[i]<dp[i-c]+w)
                dp[i]=dp[i-c]+w;
            if(dp[i]==V){
                flag=1;
                return ;
            }
        }
    }
    
    void MultiplyPack(int c,int w,int amount){
        if(c*amount>=V){
            CompletePack(c,w);
            return ;
        }
        if(flag)
            return ;
        int k=1;
        while(k<=amount){
            ZeroOnePack(k*c,k*w);
            if(flag)
                return ;
            amount-=k;
            k<<=1;
        }
        ZeroOnePack(amount*c,amount*w);
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int cases=0;
        while(1){
            int sum=0;
            for(int i=1;i<=6;i++){
                scanf("%d",&num[i]);
                sum+=i*num[i];
            }
            if(sum==0)
                break;
            if(sum%2){
                printf("Collection #%d:\n",++cases);
                printf("Can't be divided.\n\n");
                continue;
            }
            V=sum/2;
            //printf("V=%d\n",V);
            flag=0;
            memset(dp,-1,sizeof(dp));
            dp[0]=0;
            for(int i=1;i<=6;i++){
                MultiplyPack(i,i,num[i]);
                if(flag)
                    break;
            }
            //printf("flag=%d\n",flag);
            if(flag){
                printf("Collection #%d:\n",++cases);
                printf("Can be divided.\n\n");
            }else{
                printf("Collection #%d:\n",++cases);
                printf("Can't be divided.\n\n");
            }
        }
        return 0;
    }

    DFS:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    int num[7];
    int sum,half;
    int flag;
    
    void DFS(int value,int pre){
        if(flag)
            return ;
        if(value==half){
            flag=1;
            return ;
        }
        for(int i=pre;i>=1;i--){
            if(num[i]){
                if(value+i<=half){
                    num[i]--;
                    DFS(value+i,i);
                    if(flag)
                        return ;
                }
            }
        }
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int cases=0;
        while(1){
            sum=0;
            for(int i=1;i<=6;i++){
                scanf("%d",&num[i]);
                sum+=i*num[i];
            }
            if(sum==0)
                break;
            if(sum%2){
                printf("Collection #%d:\n",++cases);
                printf("Can't be divided.\n\n");
                continue;
            }
            half=sum/2;
            flag=0;
            DFS(0,6);
            if(flag){
                printf("Collection #%d:\n",++cases);
                printf("Can be divided.\n\n");
            }else{
                printf("Collection #%d:\n",++cases);
                printf("Can't be divided.\n\n");
            }
        }
        return 0;
    }
     
     
     
  • 相关阅读:
    Debug和Release版本区别
    清空模拟器中的app
    在项目中移除CocoaPods
    设置导航栏 self.navigationItem.titleView 居中
    Verify the Developer App certificate for youraccount is trusted on your device
    字典转json
    self.navigationController.navigationBar.translucent = YES航栏的属性默认 YES是透明效果并且主view不会偏移 NO是导航栏不透明 主view会向下偏移64px
    归档-对模型数组对象(存储到本地的plist文件)也数组里存放的是模型
    FMDB存储模型对象(以二进制存储)用NSKeyedArchiver archivedDataWithRootObject序列号,NSKeyedUnarchiver unarchiveObjectWithData反序列化(重点坑是sql语句@"insert into t_newsWithChannel (nwesName,newsType) values (?,?)")一定要用占位符
    根据日期计算发布时间段(NSCalendar)
  • 原文地址:https://www.cnblogs.com/jackge/p/3035341.html
Copyright © 2011-2022 走看看