POJ 1014 dividing

Source: Internet
Author: User

Analysis:

If you want to share the ball fairly, the sum of the value of the ball must be an even number. You can first determine the sum of the value of the ball. If it is an odd number, you do not need to make the following judgment.

If it is an even number, we can map this problem to a problem of multiple backpacks, and this backpack is required to be fully filled. The total capacity V of the backpack is half of the total value of all fl and sum. The cost and weight of the fl are their numbers. The number is input by the outside world. Final judgment: If the obtained F [V] is equal to sum/2, it indicates that the ball can be scored fairly.

Note that because the maximum number of balls is 20000, in extreme cases, 20000 balls are placed at a position worth 6, the maximum value of V is 6*20000/2 + 1 = 60001.

Okay, Cuihua, go to the Code:


[Cpp]
# Include <iostream>
Using namespace std;
 
# Define LEN 7
# Define INIT 0 xffffffff
Int F [60001];
 
Int max (int x, int y)
{
Return x> y? X: y;
}
 
Void ZeroOnePack (int cost, int weight, int V)
{
For (int v = V; v> = cost; -- v ){
F [v] = max (F [v], F [v-cost] + weight );
}
}
 
Void CompletePack (int cost, int weight, int V)
{
For (int v = cost; v <= V; ++ v ){
F [v] = max (F [v], F [v-cost] + weight );
}
}
 
Void MultiPack (int cost, int weight, int V, int amount)
{
If (cost * amount> = V ){
CompletePack (cost, weight, V );
Return;
}
Int k = 1;
While (k <amount ){
ZeroOnePack (cost * k, weight * k, V );
Amount-= k;
K * = 2;
}
ZeroOnePack (cost * amount, weight * amount, V );
}
 
Int main (int argc, char ** argv)
{
Int index = 0;
Int num [LEN];
While (1 ){
++ Index;
Int sum = 0;
Int bin = 0;
For (int I = 1; I <LEN; ++ I ){
Cin> num [I];
Sum + = I * num [I];
Bin | = num [I];
}
If (! Bin)
Break;
If (sum & 0x01)
Cout <"Collection #" <index <": \ n" <"Can't be divided." <endl;
Else {
Int V = sum> 1;
F [0] = 0;
For (int I = 1; I <= V; ++ I)
F [I] = INIT;
For (int I = 1; I <LEN; ++ I)
MultiPack (I, I, V, num [I]);
If (F [V]! = V)
Cout <"Collection #" <index <": \ n" <"Can't be divided." <endl;
Else
Cout <"Collection #" <index <": \ n" <"Can be divided." <endl;
}
}
System ("pause ");
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.