zoukankan      html  css  js  c++  java
  • POJ1014 Dividing

    Dividing
    Time Limit: 1000MS Memory Limit: 10000K
    Total Submissions: 71507 Accepted: 18648

    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.
    
    
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #define INF  999999999
     5 using namespace std;
     6 int dp[120050];
     7 int min(int x,int y)
     8 {
     9     return x>y? y:x;
    10 }
    11 int main()
    12 {
    13     int num[10],T=1;
    14     while(cin>>num[1]>>num[2]>>num[3]>>num[4]>>num[5]>>num[6])
    15     {
    16         int t=0;
    17         int sum=0,NUM=0;
    18         for(int i=1;i<7;i++)
    19         {
    20             if(num[i]==0) t++;
    21             sum+=num[i]*i;
    22             NUM+=num[i];
    23         }
    24         if(t==6) break;
    25         memset(dp,0,sizeof(dp));
    26         if(sum%2==0)
    27         {
    28             for(int i=0;i<=120000;i++)
    29                 dp[i]=INF;
    30             dp[0]=0;
    31             for(int i=1;i<=6;i++)
    32             {
    33                 if(num[i]==0){}
    34                 else
    35                     for(int k=1,flag=0;;k*=2)
    36                     {
    37                         if(k*2>num[i])
    38                         {
    39                             k=num[i]-k+1;
    40                             flag=1;
    41                         }
    42                         for(int j=sum/2;j>=k*i;j--)
    43                         {
    44                             dp[j]=min(dp[j],dp[j-k*i]+k);
    45                         }
    46                         if(flag)
    47                             break;
    48                     }
    49             }
    50             if(dp[sum/2]!=INF)
    51                 cout<<"Collection #"<<T++<<':'<<endl<<"Can be divided."<<endl<<endl;
    52             else
    53                 cout<<"Collection #"<<T++<<':'<<endl<<"Can't be divided."<<endl<<endl;
    54         }
    55         else
    56             cout<<"Collection #"<<T++<<':'<<endl<<"Can't be divided."<<endl<<endl;
    57     }
    58     return 0;
    59 }
    
    
    
    
    
    
    
  • 相关阅读:
    DeepLearning之路(三)MLP
    DeepLearning之路(二)SoftMax回归
    DeepLearning之路(一)逻辑回归
    自然语言处理工具
    一个 11 行 Python 代码实现的神经网络
    对联广告
    Java多线程
    QT数据库操作
    QT笔记
    C++基础入门
  • 原文地址:https://www.cnblogs.com/ISGuXing/p/7215092.html
Copyright © 2011-2022 走看看