Poj 1014 dividing (multiple backpacks)

Source: Internet
Author: User
Dividing
Time limit:1000 ms   Memory limit:10000 K
Total submissions:58921   Accepted:15200

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 wowould be easy if all the marbles had the same value, because then they coshould 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 wocould 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

Mid-Central European Regional Contest 1999

Question:

There are six marble objects whose values are 1, 2, 3, 4, 5, and 6 respectively. Then they are given the number of six marble objects and asked how to divide them evenly so that the values obtained by the two are equal.

The requirement here is equal to the balance, that is, when the size of the backpack is half of the value, the initial value of DP is negative infinity.
I started to think that the data is small (I didn't see the question clearly). In fact, the big thing is, I used the 01 backpack to do it, and decisively timed out.
So I changed to the binary optimized 01 backpack + full backpack with multiple backpacks. It is a template. It seems that only the optimized 01 backpack can be used, but I don't know how to do it.
The following is a simulation of the problem of multiple backpacks. for a total of six items, we can regard the value and cost of the first item as I. In this way, we can apply the multi-backpack template described in "9, but note that the array should be larger.
Code: 16 ms (this is the code that codeblocks runs)
#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>using namespace std;#define M 100005#define INF -99999;int sum,v[7],dp[M];void CompletePack(int cost,int wight){    int i;    for(i=cost;i<=sum;i++)        dp[i]=max(dp[i],dp[i-cost]+wight);}void ZeroOnePack(int cost,int wight){    int i;    for(i=sum;i>=cost;i--)        dp[i]=max(dp[i],dp[i-cost]+wight);}void MultiplePack(int cost,int wight,int amount){    if(cost*amount>=sum)    {        CompletePack(cost,wight);        return;    }    int k=1;    while(k<amount)    {        ZeroOnePack(k*cost,k*wight);        amount-=k;        k=k<<1;    }    ZeroOnePack(amount*cost,amount*wight);}void DP(){    int i,j,k;    for(i=0;i<=sum;i++) dp[i]=i==0?0:INF;    for(i=1;i<=6;i++)        MultiplePack(i,i,v[i]);}int main(){    int flag,i,t=0;    while(1)    {   t++;sum=0;        for(i=1;i<=6;i++)    {        cin>>v[i];        sum+=i*v[i];    }    if(!sum) break;    printf("Collection #%d:\n",t);        if(sum&1){            printf("Can‘t be divided.\n\n");            continue;        }        sum=sum>>1;        DP();        if(dp[sum]>=0)            printf("Can be divided.\n\n");        else            printf("Can‘t be divided.\n\n");    }    return 0;}

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.