There are six methods for Jquery to process text: Internal insertion, external insertion, package, replacement, deletion, and replication.

Source: Internet
Author: User

In the tug-of-war competition, the number of people on both sides cannot exceed 1. Try to bring the strength of both sides closer.

 


Backpack ideas

In the initial equation, people's "physical strength" is taken as the benefit, and at the same time as the cost.

F (I, j, k): the former I used j physical strength, and the former I used k.

F (I, j, k) = max (F (I-1, j, k), F (I-1, j-a [I], k-1) + a [I])

In this way, we can get the maximum physical strength of half, but the complexity of the cycle itself is relatively high, the addition operation has caused tle many times... Sweaty...

 


It is much faster to change to bool.

F (I, j, k) = F (I-1, j, k) or F (I-1, j-a [I], k-1)

Indicates whether the status can be transferred, and then the upper limit of j is enumerated to determine the maximum value of j when the number difference does not exceed 1. Bytes


[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
Using namespace std;
 
Bool f [22500] [51];
Int a [101], n, kk;
Int I, j, k, t;
 
Int main (){

While (scanf ("% d", & n) = 1 ){
Int ss, s = 0;
For (I = 1; I <= n; I ++ ){
Scanf ("% d", & a [I]);
S + = a [I];
}
 
Ss = s/2;
If (n & 1) kk = (n> 1) + 1;
Else kk = n> 1;
For (I = 0; I <= ss; I ++) for (j = 0; j <= kk; j ++) f [I] [j] = false;
 
F [0] [0] = true;
For (I = 1; I <= n; I ++)
For (k = min (kk, I); k> 0; k --)
For (j = ss; j> = a [I]; j --)
If (f [j-a [I] [k-1]) f [j] [k] = true;
 
While (ss ){
If (n % 2 = 0 & f [ss] [n/2]) break;
If (n % 2! = 0 & f [ss] [n/2 + 1]) break;
If (n % 2! = 0 & f [ss] [n/2]) break;
Ss --;
}
Printf ("% d \ n", ss, s-ss );
}
 
Return 0;
}

# Include <cstdio>
# Include <cstring>
# Include <algorithm>
Using namespace std;

Bool f [22500] [51];
Int a [101], n, kk;
Int I, j, k, t;

Int main (){

While (scanf ("% d", & n) = 1 ){
Int ss, s = 0;
For (I = 1; I <= n; I ++ ){
Scanf ("% d", & a [I]);
S + = a [I];
}

Ss = s/2;
If (n & 1) kk = (n> 1) + 1;
Else kk = n> 1;
For (I = 0; I <= ss; I ++) for (j = 0; j <= kk; j ++) f [I] [j] = false;

F [0] [0] = true;
For (I = 1; I <= n; I ++)
For (k = min (kk, I); k> 0; k --)
For (j = ss; j> = a [I]; j --)
If (f [j-a [I] [k-1]) f [j] [k] = true;

While (ss ){
If (n % 2 = 0 & f [ss] [n/2]) break;
If (n % 2! = 0 & f [ss] [n/2 + 1]) break;
If (n % 2! = 0 & f [ss] [n/2]) break;
Ss --;
}
Printf ("% d \ n", ss, s-ss );
}

Return 0;
}


 

Related Article

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.