Description
Two hundred silver dollars from the loser ."
"Being the most powerful man in the country,
The king has so nice horses that in each class his horse is better than Tian's.
As a result,
Each time the King takes six hundred silver dollars from Tian ."
Finding the maximum matching in a bipartite graph.
Draw Tian's horses on one side,
And the King's horses on the other.
Whenever one of Tian's horses can beat one from the king,
We draw an edge between them,
Meaning we wish to establish this pair.
Then, the problem of winning as your rounds as possible is just
To find the maximum matching in this graph.
If there are ties, the problem becomes more complicated,
He needs to assign weights 0, 1, or-1 to all the possible edges,
And find a maximum weighted perfect matching...
However,
The horse racing problem is a very special case of bipartite matching.
The graph is decided by the speed of the horses ---
A vertex of higher speed always beat a vertex of lower speed.
In this case,
The weighted bipartite matching algorithm is a too advanced tool
To deal with the problem.
In this problem,
You are asked to write a program to solve this special case of matching problem.
Input
The input consists of up to 50 test cases.
Each case starts with a positive integer n (n <= 1000) on the first line,
Which is the number of horses on each side.
The next n integers on the second line are the speeds of Tian ~s horses.
Then the next n integers on the third line are the speeds of the King's horses.
The input ends with a line that has a single 0 after the last test case.
Output
For each input case,
Output A line containing a single number,
Which is the maximum money Tian Ji will get, in silver dollars.
Sample Input
3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
0
Sample output
200
0
0
Classic greedy, well-known, Tianji, horse racing, Tianji, wins two hundred for each game, and loses two hundred for the opposite.
When solving the problem, consider not only the fastest horse, but also the slowest horse.
1. When Tian Ji's slowest horse is faster than Qi Wang's slowest horse, he wins the first game.
2. When Tian Ji's slowest horse was slower than Qi Wang's slowest horse, he lost a match with Qi Wang's fastest horse.
3. If the slowest horse speed is equal
One. When Tian Ji's fastest horse is faster than Qi Wang, he wins first.
Two. When Tian Ji's fastest horse was slower than Qi Wang, he lost one game with the slowest horse and Qi Wang's fastest horse.
Three. when Tian Ji's fastest horse is equal to Qi Wang's fastest horse, he takes the slowest horse and Qi Wang's fastest horse. (the slowest case of Tian Ji is equal to that of Qi Wang, and the fastest case of Tian Ji is slower than Qi Wang)
#include<stdio.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>#include<queue>#include<stack>#define mem(a,b) memset(a,b,sizeof(a))#define ll __int64#define MAXN 1000#define INF 0x7ffffff#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;int s1[1000+10];int s2[1000+10];int vis[1000+10];int main(){ int n,m,i,j; while(scanf("%d",&n)!=EOF) { if(n==0) break; mem(vis,0); int ans=0; for(i=0;i<n;i++) scanf("%d",&s1[i]); for(i=0;i<n;i++) scanf("%d",&s2[i]); sort(s1,s1+n); sort(s2,s2+n); int l1=0,r1=n-1; int l2=0,r2=n-1; while(l1<=r1) { if(s1[l1]>s2[l2]) { ans++; l2++; l1++; } else if(s1[l1]<s2[l2]) { ans--; l1++; r2--; } else { if(s1[r1]>s2[r2]) { ans++; r1--; r2--; } else if(s1[r1]<=s2[r2]) { if(s1[l1]<s2[r2]) ans--; l1++; r2--; } } } printf("%d\n",ans*200); } return 0;}