cat and dog fight

The cat and dog war of the new year will be contested through the classic game SC (StarCraft). The feud of wild cats and flying dogs have been preparing for this for a long time. In order to make the war more difficult and dramatic, the two sides agreed to choose Terran. (Human) and can only make machine gunners.
The game started, and soon, the Wildcats had gathered a few teams of machine gunners and launched a tentative attack; however, the number of machine gunners in Flying Dog was already quite large. The soldiers of the wild cat and the flying dog met at the door of the flying dog, so there was a bloody storm and screams. Since it was at the door of Flying Dog's house, Flying Dog's troops would be replenished very quickly, and the Wildcat could not see the enemy and decided to retreat. At this time, Flying Dog's troops were not enough, so they didn't catch up.
Since it is not allowed to build doctors, the Machine Gunners have no way to replenish their blood. The wounded soldier had to endure. 555 -
Now, the Wildcats have gathered enough troops to launch a second attack. To make this attack a bigger blow to the dogs, the Wildcats decided to split the existing pawns into two and attack from two lanes. Since some soldiers were wounded in the first battle, in order to make the strength of the two parts more even, the rules of division are as follows: 1 ) The number of soldiers in the two parts can only differ by at most one; 2 ) The blood of each part of the soldiers The sum of the values ​​must be as close as possible. Now please write a program that, given the number of pawns the wild cat has now and the health value of each pawn, find the sum of the health value of each pawn after the wild cat is divided into two parts according to the above rules.
Input and output format
Input format:
The first line is an integer n ( 1 <=n<= 200 ), which represents the number of machine gunners that Wildcat has now. The following n lines each contain an integer representing the HP of each Marine ( 1 <= ai <= 40 ).
Output format:
One line, two integers, indicating the sum of the blood value of each soldier after being divided into two parts
Input and output example
Input Sample # 1 : 
 3 
35 
20 
32 
Output Sample # 1 : 
 35  52
Topic description

Solution: The maximum number of troops in each part is 100.

      The maximum HP value is 100*40=4000,

We can carry out knapsack, preprocessing out

All hp values ​​j that can be formed by individual i, that is, f[j][i]

Then according to the obtained f[j][i], enumerate the blood value of the first part,

to obtain the optimal solution.

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<string>
 5 #include<cstdio>
 6 #include<cmath>
 7 #include<queue>
 8 #define inf 100000000
 9 using namespace std;
10 int n,f[80010][200];
11 int sum,ans1,ans2;
12 int main()
13 {
14     scanf("%d",&n);
15     f[0][0]=1;
16     for(int i=1,x;i<=n;++i)
17     {
18        scanf("%d",&x);
19        for(int j=8000;j>=x;--j)
20         for(int k=100;k>=1;--k)    
21          f[j][k]=f[j][k]||f[j-x][k-1];
22        sum+=x;
23     } 
24     int dep=1e9;
25     for(int j=sum;j>=0;--j)
26      if(f[j][n/2] && f[sum-j][n-n/2])
27      {
28          int l=min(j,sum-j),r=max(j,sum-j);
29          if(r-l<dep) ans1=l,ans2=r,dep=r-l;
30      }
31     cout<<ans1<<" "<<ans2; 
32     return 0;
33 }
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324706106&siteId=291194637
Dog
Recommended