Tian Ji horse racing

Topic description
There is a famous story in the history of our country: that was 2300 years ago. Tian Ji, the great general of Qi, liked to race horses. He often races horses with the King of Qi. He and King Qi both have three horses: regular horses, superior horses, and super horses. There are three rounds in total, and the winner of each round can get 200 credits from the loser. Each horse can only be used once. King Qi's horses are good, and for horses of the same level, King Qi's horses are always better than Tian Ji's. So every time he races with King Qi, Tian Ji always loses 600 silver coins.
Tian Ji was very depressed until he met the famous military advisor - Sun Bin. After Tian Ji adopted Sun Bin's strategy, after three games, he won 200 silver coins from King Qi easily and gracefully. This is really a very simple tactic. Since King Qi always rides the best horse first, then the next best horse, so Tian Ji uses his regular horses to match the king's super horses, his own super horses to match the king's superior horses, and his superior horses to match the king's regular horses. , to win 200 credits with a 2-1 record. It's really simple.
What if there are more than three horses? This problem can obviously be transformed into a bipartite graph best matching problem. Put Tian Ji's horse on the left and King Qi's horse on the right. Between Tian Ji's horse A and King Qi's B, if Tian Ji's horse wins, there will be a side with a weight of 200; if there is a draw, there will be a side with a weight of 0; if he loses, there will be a side with a weight of -200. Side... If you can't find the best match, you can use the minimum cost maximum flow. However, the horse racing problem is a special problem of optimal matching of bipartite graphs. The above algorithm is too advanced, and it is simply a knife to kill a chicken. Now, please design a simple algorithm to solve this problem.
Input and output format
Input format:
The first line contains an integer n, indicating how many horses they each have (both have the same number of horses). The second line contains n integers, each of which represents the speed value of a certain horse of Tian Ji ( 0 <= speed value <= 100 ). The third line contains n integers, describing the speed of King Qi's horse. When two horses meet, according to the size of the speed value, you can know which horse will win. If the speed values ​​are the same, then the game is tied and no one takes money.
【Data scale】
For 20 % of the data, 1 <=N<= 65 ;
For 40 % of the data, 1 <=N<= 250 ;
For 100 % of the data, 1 <=N<= 2000 .
Output format:
Only one line, an integer, indicates how many silver coins Tian Ji can get at most.
Input and output example
Input Sample # 1 : 
 3 
92  83  71 
95  87  74 
Output Sample # 1 : 
 200
topic

greedy,

Like a war of attrition, oneself a, the other party b

If a's best horse is stronger than b's best horse, directly compare

If a's worst horse is better than b's best and worst horse, directly

If neither, compare a's worst horse to b's best horse

(๑′ᴗ‵๑) I Lᵒᵛᵉᵧₒᵤ❤

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<string>
 7 using namespace std;
 8 const int N=3000+20;
 9 int n,l1,r1,l2,r2,a[N],b[N];
10 int ans;
11 int main()
12 {
13     scanf("%d",&n);
14     for(int i=1;i<=n;++i) scanf("%d",&a[i]);
15     for(int i=1;i<=n;++i) scanf("%d",&b[i]);
16     l1=l2=1;r1=r2=n;
17     sort(a+1,a+n+1);sort(b+1,b+n+1);
18     for(int o=1;o<=n;o++)
19     {
20         if(a[r1]>b[r2]) r1--,r2--,ans++;
21         else if(a[l1]>b[l2]) l1++,l2++,ans++;
22             else{
23                 if(a[l1]<b[r2]) ans--;
24                 l1++;r2--;
25             }
26     }
27     cout<<ans*200;
28     return 0;
29 }
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324621543&siteId=291194637