problem DescriptionHere is a famous stories in Chinese.
"That's about 2300 years ago." General Tian Ji is a high official in the country Qi. He likes to play horse racing with the king and others. "
"Both of Tian and the king had three horses in different classes, namely, regular, plus, and super. The rule is to has three rounds in a match; Each of the horses must is used in one round. The winner of a single round takes and hundred silver dollars from the loser. "
"The Being the most powerful man in the country, the king had so nice horses, and each class he horse is better than Tian ' S. As a result, each time the king takes six hundred silver dollars from Tian. "
"Tian Ji is not happy on that, until he met Sun Bin, one of the most famous generals in Chinese history. Using a little trick due to Sun, Tian Ji brought home of 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 the Super class from the king, they would certainly lose that round. But then he plus beat the King ' s regular, and his super beat the King's Plus. What's a simple trick. And how does your think of Tian Ji, the high ranked official in China? "
Were Tian Ji lives in nowadays, he'll certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he could discover that the horse racing problem can be simply Viewe D as 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 a edge between them, meaning we wish to establish this Pair. Then, the problem of winning as many rounds as possible are just to find the maximum matching in this graph. If There is 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 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 of matching problem.
InputThe input consists of up to test cases. Each case starts with a positive an integer n (n <=) on the first line, which are the number of horses on each side. The next n integers on the second line is the speeds of Tian ' s horses. Then the next n integers in the third line is the speeds of the King ' s horses. The input ends with a line is has a single 0 after the last test case.
OutputFor each input case, output a line containing a single number, which are the maximum money Tian Ji'll get, in silver Dollars.
Sample Input
392 83 7195 87 74220 2020 20220 1922 180
Sample Output
20000
Test instructions: Tian Bogey horse racing. Tian Bogey and King each have n horse, enter the field bogey horse speed and King horse speed. Each round of field bogey win 2002 silver, flat to get 2, lost 2002 silver. Ask Tian bogey how much you can get.
This problem can be used DP, to each horse connected to the right to a binary graph best match, greedy.
Here's a greedy way to talk about:
1. When Tian bogey slowest Mabizi the slowest horse fast, win a first
2. When Tian bogey slowest Mabizi slowest horse slow, and king the fastest ponies, lose a
3. When Tian bogey fastest Mabizi the fastest horse fast, win a first.
4. When Tian bogey fastest Mabizi the fastest horse slow, take the slowest horse and King fastest ponies, lose one.
5. When Tian bogey fastest horse and King fastest Minamisoma etc, take the slowest Malay and king the fastest horse ratio.
The correctness of the greedy horse racing is proved.
First, the proof of the simple situation:
1. When Tian bogey slowest Mabizi the slowest horse fast, win a first. Because always want to win King the slowest horse, it is better to win it with the most useless Malay.
2. When Tian bogey slowest Mabizi slowest horse slow, and King fastest ponies, lose one. Because Tian Ji the slowest horse always to lose, it is better to use it to consume King most useful horse.
3. When the slowest and king slowest horses are equal, the 4 and 5 are discussed.
4. When Tian bogey fastest Mabizi the fastest horse fast, win a first. Because the fastest horse is used to win others fast horse, others slow horse what horse can win.
5. When Tian bogey fastest Mabizi the fastest horse slow, take the slowest horse and king the fastest ponies, lose a one, because anyway to lose a, rather than the most useless horse lose.
6. When Tian bogey the fastest horse and king the fastest minamisoma and so on, this is going to start the discussion, the greedy method is to take the slowest Malay and king the fastest horse ratio.
Before the proof like the axiom, we can see all agree, no objection, will not elaborate.
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
According to the preceding "axiom" or inductive hypothesis, there is an 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 fastest ponies have the best solution, known when the n=2 is established, so for n>2 and for the whole (nonsense, the number of horses is of course an integer) is also established. When N=1 .... This does not seem to be discussed.
Examples:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace Std;bool cmp (int a,int b) {return a>b;} int main () {int N,money; int a[1000],b[1000]; while (cin>>n) {if (!n) break; for (int i=0;i<n;i++) cin>>a[i]; for (int i=0;i<n;i++) cin>>b[i]; Sort (a,a+n,cmp); Sort (b,b+n,cmp); money=0; for (int i=n-1;i>=0;i--) {if (A[i]>b[i]) {money++; } else if (A[i]<b[i]) {money--; for (int j=0;j<i;j++) b[j]=b[j+1]; } else if (A[i]==b[i]) {if (a[0]>b[0]) {money++; for (int j=0;j<i;j++) {a[j]=a[j+1]; B[J]=B[J+1]; }} else if (A[0]<b[0]) {money--; for (int j=0;j<i;j++) b[j]=b[j+1]; } else {if (a[i]>b[0]) {Mon ey++; for (int j=0;j<i;j++) b[j]=b[j+1]; } else if (A[i]<b[0]) {money--; for (int j=0;j<i;j++) b[j]=b[j+1]; }}}} cout<<money*200<<endl; } return 0;}
C language Greedy (2) ___ Tin bogey Race (Hdu 1052)