Assume that all the watermelons are loaded with asum, And the request is packed with asum/2 backbacks.
At the beginning, we started to use greedy, WA. Later, I used a 01 backpack. As a result, TLE had too much data. I used deep search!
DFS (INT sum, int I) indicates that sum is installed, and the number I is determined.
It took more than 1200 ms, and I don't know how big cows made it in 60 ms. It's 20 times that of coal! Optimize it later.
The Code is as follows:
# Include <iostream> # include <cstdio> # include <math. h ># include <algorithm> using namespace STD; int A [25], asum, mi; void DFS (INT sum, int I) {if (I <0) // After the watermelon has been installed, return; int T = FABS (asum-2 * sum); // The weight difference between the two siblings if (T <mi) mi = T; If (2 * sum-asum> = mi) // pruning, the weight difference is greater than or equal to the minimum value, and the search is greater than or equal to return; DFS (sum + A [I], I-1); // installs the I-th DFS (sum, I-1); // does not hold the I-th} int main () {int N; while (scanf ("% d", & N )! = EOF) {asum = 0; MI = 99999999; For (INT I = 0; I <n; I ++) {scanf ("% d ", & A [I]); asum + = A [I];} DFS (0, n-1); // never load anything, start cout For The Last watermelon decision <mi <Endl;} return 0 ;}
View code