AC Diary--dividing POJ 1014

Source: Internet
Author: User

Dividing
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 69575 Accepted: 18138

Description

Marsha and Bill own a collection of marbles. They want to split the collection among themselves so, both receive an equal share of the marbles. This would is 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 is 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, each of the them gets the same total value. Unfortunately, they realize that it might is impossible to divide the marbles on this the-to Marbles is even). For example, if there is one marble of value 1, one of value 3 and both of the value 4, then they cannot is split into sets of Equal value. So, they ask for 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 is described by the Input-line "1 0 1 2 0 0". The maximum total number of marbles would be 20000.
The last line of the input file would 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

Sample Output

Collection #1: Can ' t be divided. Collection #2: Can be divided.

Source

Mid-Central European Regional Contest 1999 topic: There are many items, but the total is 20000. Then the value of each item in the 1~6 between the two groups of data each set of data output value for the number of items 1~6 and then call you to determine whether you can be divided into the value of the items of the idea: backpack DP, difficult, and this I submitted 4 times before, two times tle, once the pre this pit many read the title, very  Many people may think that this is a search problem and then start a brute force search, and the final tle becomes * *. Let's analyze the solution of this problem first, it wants you to divide the goods into two piles, and the value is equal so, we have an optimization when the total value of the item is%2==1, it is determined that it cannot be divided evenly if%2==0 we start DP This DP is a bare multi-backpack DP We push with the value of the current item   Whether the value exists value for the item of I the current level of goods by value for the i-1 of the current level is launched then determine whether the value of the item total value/2 exists just fine because the total value of the goods/2 can be pieced out, then the other half must be able to the contrary can not but a bit of a pit if you really are running down, So we have to determine in the DP process whether there is an item total value/2 There is such a small optimization will not timeout can be AC feel should have a better solution, but I can't think of it.  、、。 There's one more thing. Pre to note that there is a line break between each set of data, on the code:
#include <cstdio>#include<cstring>using namespacestd;inta[7],sum_value=0, ci[7][20001],now=0;BOOLdp[7][120000];BOOLCheck_answer () {memset (DP,false,sizeof(DP)); dp[0][0]=true;  for(intI=1; i<=6; i++)    {         for(intj=0; j<=sum_value;j++)        {            if(Dp[i][sum_value])return true; if(dp[i-1][j]) { for(intv=0; v<=a[i];v++) dp[i][j+ci[i][v]]=true; }        }    }    if(dp[6][sum_value])return true; Else return false;}intMain () {BOOLIf_break;  while(1) {If_break=true, sum_value=0, now++;  for(intI=1; i<=6; i++) {scanf ("%d",&A[i]); if(A[i]) if_break=false; Sum_value+=a[i]*i;  for(intj=1; j<=a[i];j++) ci[i][j]=i*J; }        if(If_break) Break; if(sum_value%2) {printf ("Collection #%d:\ncan ' t be divided.\n\n", now); Continue; } sum_value/=2; if(Check_answer ()) printf ("Collection #%d:\ncan be divided.\n\n", now); Elseprintf"Collection #%d:\ncan ' t be divided.\n\n", now); }    return 0;}

Another failure code attached to me:

#include <cstdio>#include<cstring>using namespacestd;inta[7],sum_value,num,ci[20001],flag;BOOLdp[70000];intMain () {intnow=0;  while(1) { now++; Memset (DP,false,sizeof(DP)); Sum_value=0, num=0, flag=1; BOOLif_break=true;  for(intI=1; i<=6; i++) {scanf ("%d",&A[i]); Sum_value+=a[i]*i,num+=A[i]; if(a[i]!=0) {If_break=false;  for(intj=flag;j<flag+a[i];j++) ci[j]=i; Flag+=A[i]; }        }        if(If_break) Break; if(sum_value%2) {printf ("Collection #%d:\ncan ' t be divided.\n\n", now); Continue; } sum_value/=2; intmax_now=0; dp[0]=true;  for(intI=1; i<=num;i++)        {            intmax_now_=Max_now;  for(intj=0; j<=max_now;j++)            {                if(Dp[j]) {dp[j+ci[i]]=true; if(j+ci[i]==sum_value) {printf ("Collection #%d:\ncan be divided.\n\n", now); If_break=true;  Break; }                    if(J+ci[i]>max_now_&&j+ci[i]<=sum_value) max_now_=j+Ci[i]; }} Max_now=Max_now_; if(If_break) Break; }        if(If_break)Continue; printf ("Collection #%d:\ncan ' t be divided.\n\n", now); }    return 0;}
View Code

AC diary--dividing POJ 1014

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.