TZOJ 1503 Incredible Cows (binary search bipartite +)

description

Farmer John is well known for his great cows. Recently, the cows have decided to participate in the Incredible Cows Puzzle Contest (ICPC).

Farmer John wants to divide the cows into two teams, and he wants to minimize the difference of Puzzle Solving Power of two teams.

Puzzle Solving Power of a team is sum of Puzzle Solving Power of cows forming that team.

Help F.J. to find the minimum difference!

Entry

The first line of input consists of a single integer T, the number of test-cases. Each test-case consists of a line containing n (2 <= n <= 34), number of cows. n lines follow. i-th line contains the Puzzle Solving Power of i-th cow. Puzzle Solving Power of a cow is a non-negative number less than 10,000,000. There is a blank line between two consecutive test-cases.

Export

For each test-case, output a line containing the minimum difference which can be achieved.

Sample input

2
3
12
6
6

10
123
455
1000
403
234
554
129
454
84
11

Sample Output

0
5

The meaning of problems

The number n into two piles, and the piles such that the difference between the minimum.

answer

n up to 34, the direct blast search certainly not. 17 but may be directly blast search (2 ^ 17).

Consequently, the array is equivalent to half open, and is referred to and suml SumR, then burst seized all cases the piles.

Then the question is paired, assuming the first stack taken with X, taken out of the second stack Y.

The answer is to minimize the function | (X + Y) - ((suml-X) + (sumr-Y)) |, simplifying get | 2X + 2Y-suml-sumr |.

We enumerate the unknown X, then the absolute value function becomes, readily available Y> = (suml + sumr) / 2-X, Y. Then find the smallest lower_bound

This is a minimum number of cases in front of the right, as well as in the case of the left is to get half of Y.

The time complexity of O (2 ^ 17 * log (2 ^ 17)).

Code

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define ll long long
 5 ll a[35];
 6 set<ll>se[2];
 7 void dfs(int s,int t,bool f,ll sum)
 8 {
 9     if(s>t)
10     {
11         se[f].insert(sum);
12         return;
13     }
14     dfs(s+1,t,f,sum+a[s]);
15     dfs(s+1,t,f,sum);
16 }
17 int main()
18 {
19     int n,t;
20     cin>>t;
21     while(t--)
22     {
23         se[0].clear(),se[1].clear();
24         cin>>n;ll suml=0,sumr=0;
25         for(int i=1;i<=n/2;i++)cin>>a[i],suml+=a[i];
26         for(int i=n/2+1;i<=n;i++)cin>>a[i],sumr+=a[i];
27         dfs(1,n/2,0,0);dfs(n/2+1,n,1,0);
28         ll min_abs=1e18;
29         set<ll>::iterator it,itt;
30         for(auto X:se[0])
31         {
32             it=se[1].lower_bound((suml+sumr)/2-X);
33             itt=it;
34             if(it!=se[1].begin())--itt;
35             if(it==se[1].end())--itt,--it;
36             min_abs=min(min_abs,min(abs(suml+sumr-2*X-2*(*it)),abs(suml+sumr-2*X-2*(*it))));
37         }
38         cout<<min_abs<<'\n';
39     }
40     return 0;
41 }

Guess you like

Origin www.cnblogs.com/taozi1115402474/p/11960263.html