Tian Ji--The Horse Racing
Description
Here's a famousstory in Chinese.
That was about2300 years ago. General Tian Ji is a high official in the country Qi. He Likesto play horse racing with the king and others.
Both of Tian and the king has three horses in different classes, namely,regular, plus, and super. The rule is to has three rounds in a match; Each ofthe horses must is used in one round. The winner of a round takes twohundred silver dollars from the loser.
Being the most powerful mans in the country, the king had so nice horses that Ineach class he horse is better than Tian ' s. As a result, each time the Kingtakes six hundred silver dollars from Tian.
Tian Ji was wasn't happy about that, until he met Sun Bin, one of the most famousgenerals in Chinese history. Using a little trick due to Sun, Tian Ji broughthome both hundred silver dollars and such a grace in the next match.
It is a rather simple trick. Using the He regular class horse race against Thesuper class from the king, they would certainly lose that round. But then Hisplus beat the King's regular, and his super beat the King ' s plus. What a simpletrick. And how does your think of Tian Ji, the high ranked official in?
Were Tian Ji lives in nowadays, he'll certainly laugh at himself. Even more,were he sitting in the ACM contest right now, he may discover that the horseracing problem can be simply viewed As finding the maximum matching in abipartite graph. Draw Tian ' s horses on one side, and the king's Horses on Theother. Whenever one of Tian ' s horses can beat one from the king, we draw anedge between them, meaning we wish to establish this P Air. Then, the problem ofwinning as many rounds as possible are just to find the maximum matching in thisgraph. If There is ties, the problem becomes more complicated, he needs toassign weights 0, 1, or-1 to all the possible edges, And find a maximum weightedperfect 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 speedalways beat a vertex of lower speed. In this case, the weighted bipartitematching algorithm are a too advanced tool to deal with the problem.
In this problem, you is asked to the write a program to solve this special case ofmatching problem.
Input
The input consistsof up to the test cases. Each case starts with a positive an integer n (n<=1000) on the first line, and which is the number of horses on each side. Thenext n integers on the second line is the speeds of Tian ' s horses. Then thenext n integers in the third line is the speeds of the King ' s horses. Theinput ends with a line, have a single ' 0 ' after the last Test case.
Output
For each inputcase, output a line containing a single number, which are the maximum money Tianji would get, in silver dollar S.
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
The title means: Tian Bogey horse racing. Give the field bogey and King horse number as well as the speed of horses, find the biggest number of field bogey win the game, and then multiply 200, is the end of Tian bogey can win the money.
Problem Solving idea: can use greedy algorithm.
Greedy strategy: can win a win, can not win on the consumption of Qi Wang horse the fastest one.
Game ideas:
First, the speed of the king and the horses are sorted from small to large.
First compare Tian bogey and king the slowest horse speed.
A, if Tian bogey slowest Mabizi the slowest horse fast, then use field bogey slowest horse and King slowest ponies, win A.
B, if Tian bogey slowest Mabizi slowest horse slow, then no matter how, Tian bogey will lose one, therefore, with Tian bogey slowest horse and King the fastest horse race, lose one, consume King the fastest horse.
C, if the slowest horse and king the slowest horse speed is equal, then divided into two situations discussed:
C1, if Tian bogey fastest Mabizi fastest horse fast, then use Tian bogey the slowest horse on the king of the slowest horse, with Tian bogey the fastest horse on the king of the fastest horse, so as to ensure a win a flat .
C2, if Tian bogey the fastest horse than King fastest horse, then with Tian bogey the slowest horse and king the fastest ponies, lose one, consume King the fastest horse.
In addition to the above situation C2 are well understood, the following detailed explanation C2. In C2, Tian bogey the fastest horse and king the fastest horse can be divided into two cases, Tian bogey fastest Mabizi the fastest horse slow and equal. If Tian bogey fastest Mabizi the fastest horse slow, obviously C2 can find the optimal solution. One of the remaining cases is shown below
Proof: Tian bogey fastest horse and King fastest Minamisoma and so on when the slowest Malay and King fastest ponies have the best solution.
1) Suppose they have n horses, when they look at the n=2.
A1 A2
B1 B2
Because Tian bogey the fastest horse and king the fastest minamisoma so a1=b1,a2=b2 so this situation has 2 kinds of game way, easy to get the two ways to score equal.
2) When the sequence A and Series B are all equal (A1=B1,A2=B2...AN=BN), obviously the slowest Malay and King fastest ponies have the best solution, can win n-1 long, lose 1, can't find a better side
The law.
3) When the sequence A and the B elements are all equal (A1=B1=A2=B2...=AN=BN), can not win or lose.
Now suppose that n horses take the slowest Malay and King fastest ponies have the best solution, proving that there are n+1 horses with the slowest Malay and King fastest ponies also have the optimal solution.
Sequence
A1 A2 A3 a4...an an+1
B1 B2 B3 b4...bn bn+1
where ai>=ai-1,bi>=bi-1
When series A and Series B are not all equal, take the slowest Malay and king the fastest horse-to-be series.
(A1) A2 A3 a4...an an+1
B1 B2 b3 b4...bn (bn+1)
4 Kinds of situation discussion
1.b1=b2,an=an+1
Then there are
A2 A3 a4...an
B2 B3 b4...bn
Where a2>=a1,a1=b1,b1=b2, get a2>=b2 (after this size relationship no longer discussed), An>=bn.
At this time, if a2=b1, according to inductive hypothesis, there is an optimal solution, otherwise a2> according to the preceding "axiom" argument has the optimal solution.
When and only if the a sequence, b sequence elements are all equal when there is AN+1=B1, has been proven, so an+1>b1, win back the slowest Malay and king the fastest horse than the loss of that one.
2.b1<=b2,an=an+1
Swap the location of the B1,B2,
Sequence
(A1) A2 A3 a4...an an+1
B2 B1 b3 b4...bn (bn+1)
At this time a2>=a1,an>=bn,
For child tables
A2 A3 a4...an
B1 B3 b4...bn
have the optimal solution.
AN+1>=B2, when and only if b2=b3=b4=. =bn+1 when there is an+1=b2, in this case, the other elements of a <=b1,b2,b3,b4. BN, for this part, can win X (x<=n), if not take the slowest Malay and King fastest ponies to take the fastest Malay and king the fastest ponies, at this time a plate, can win x-1 disk, and take the slowest Malay and king the fastest ponies, lose a disk can win X disk, in general, or x this number, no loss.
3.b1=b2,an<=an+1
4.b1<=b2,an<=an+1 proof method is similar, no repetition.
To be properly n+1 horse, Tian Bogey and king the fastest and slowest horse speed is equal, take the slowest Malay and king the fastest ponies have the best solution, known when the n=2 is established, so for the n>2 and is an integer is also established.
The program code is:
2287.cpp: Defines the entry point of the console application.
//
#include <iostream>
#include <algorithm>
Using NAMESPACESTD;
#define MAXHOURSES1005//maximum number of horses
inttianjihourses[maxhourses];//Tian Bogey Horse speed
intkinghourses[maxhourses];//King Horse Speed
int n;//number of horses entered
void result ();
int main ()
{
cin>>n;
while (n!=0)
{
Result ();
cin>>n;
}
return 0;
}
void result ()
{
for (int i=0;i<n;i++)
cin>>tianjihourses[i];
for (int i=0;i<n;i++)
cin>>kinghourses[i];
Sort (tianjihourses,tianjihourses+n);//sorting
Sort (kinghourses,kinghourses+n);//sorting
int loss=0;
int win=0;
int tleft=0,tright=n-1;
int kleft=0,kright=n-1;
while (Tleft<=tright)
{
if (Tianjihourses[tleft]>kinghourses[kleft])
{
Tian bogey slowest Mabizi the slowest horse fast
Win+=1;
Tleft+=1;
Kleft+=1;
}
ElseIf (Tianjihourses[tleft]<kinghourses[kleft])
{
Tian bogey slowest Mabizi slowest horse slow
Loss+=1;
Tleft+=1;
kright-=1;//consumes the fastest horse
}
Else
{
Tian bogey the slowest Mabizi the slowest horse at the same speed
if (Tianjihourses[tright]>kinghourses[kright])
{
Tian bogey the fastest horse faster than the King fastest horse
Win+=1;
Tright-=1;
Kright-=1;
}
Else
{
if (Tianjihourses[tleft]<kinghourses[kright])
{
Tian bogey the fastest horse speed than the king fastest horse speed slow
Loss+=1;
}
Tleft+=1;
kright-=1;//consumes the fastest horse
}
}
}
cout<<200* (Win-loss) <<endl;
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Ian Ji, the greedy algorithm-the Horse Racing