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;
}