Hdu1059 monotonous queue optimization multiple backpacks O (VN)

Source: Internet
Author: User

Dividing

Time Limit: 2000/1000 MS
(Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 3850 accepted submission (s): 1035


Problem description

Marsha and Bill own a collection of marbles. They want
Split the collection among themselves so that both receive an equal share
The marbles. This wocould be easy if all the marbles had the same value, because
Then they cocould just split the collection in half. But unfortunately, some
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
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 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 ''; 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.

 

 

Divide several stones of different values into two equal values.
,
It can be converted into multiple knapsack problems
,
Set the value and cost of an item as the value of a stone.
,
If you can fill a backpack with half its total capacity
,
Then we can divide it into two equal parts.
.
The multiple backpacks here use monotonous queue optimization.
DP
Algorithm
.

 

The Code is as follows:
:

# Include <stdio. h> <br/> # include <string. h> <br/> # define maxn 120005 <br/> struct queue <br/>{< br/> int num, value; <br/>}que [maxn]; <br/> int head, tail; <br/> int DP [maxn]; <br/> void enqueue (int x, int y) <br/>{< br/> while (que [tail]. value <Y & head <= tail) tail --; <br/> que [++ tail]. num = x; <br/> que [tail]. value = y; <br/>}< br/> int main () <br/>{< br/> int I, j, D, sum, CAS; <br/> int C [7]; <br/> CAS = 0; <br/> while (1) <br/>{< br/> CAS ++; <br/> sum = 0; <br/> for (I = 1; I <= 6; ++ I) <br/>{< br/> scanf ("% d", & C [I]); <br/> sum + = C [I] * I; <br/>}< br/> If (sum = 0) break; <br/> If (sum % 2 = 1) <br/> {<br/> printf ("collection # % d:/ncan't be divided. /n ", CAS); <br/> continue; <br/>}< br/> else sum/= 2; <br/> memset (DP, 0, sizeof (DP); <br/> for (I = 1; I <= 6; ++ I) <br/> {<br/> If (C [I] = 0) continue; <br/> If (C [I]> sum/I) c [I] = sum/I; <br/> for (D = 0; D <I; ++ d) <br/>{< br/> head = 1; tail = 0; <br/> for (j = 0; j <= (Sum-d)/I; ++ J) <br/> {<br/> enqueue (J, DP [J * I + D]-J * I); <br/> while (que [head]. num <j-C [I]) Head ++; <br/> DP [J * I + D] = que [head]. value + J * I; <br/>}< br/> If (DP [Sum] = sum) printf ("collection # % d:/ncan be divided. /n ", CAS); <br/> else printf (" collection # % d:/ncan't be divided. /n ", CAS); <br/>}< br/> 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.