Greedy C language (2) ___ Tian Ji Horse Racing (Hdu 1052)
Problem DescriptionHere is a famous story in Chinese history.
"That was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others ."
"Both of Tian and the king have three horses in different classes, namely, regular, plus, and super. the rule is to have three rounds in a match; each of the horses must be used in one round. the winner of a single round takes 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."
"Tian Ji was not happy about 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 two hundred silver dollars and such a grace in the next match."
"It was a rather simple trick. using his regular class horse race against the super class from the king, they will certainly lose that round. but then his plus beat the king's regular, and his super beat the king's plus. what a simple trick. and how do you think of Tian Ji, the high ranked official in China? "
Were Tian Ji lives in nowadays, he will certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he may discover that the hZ limit? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> keys =" 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.
InputThe 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.
OutputFor each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.
Sample Input
392 83 7195 87 74220 2020 20220 1922 180
Sample Output
20000
Question: Tian Ji horse racing. Tian Ji and Qi Wang each have n horses. The input speed of Tian Ji's horse is the same as that of Qi Wang's horse. Each round of Tianji won 200 yuan in silver, and even 0 yuan in silver, and lost 200 yuan in silver. Ask Tian Ji how much he can get.
This question can be changed to optimal matching and greedy for a bipartite graph by using DP and assigning power to each horse link.
Here is the greedy method:
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. When Tian Ji's fastest horse is faster than Qi Wang's, he wins the first game.
4. When Tian Ji's fastest horse was slower than Qi Wang's fastest horse, he lost the slowest horse and Qi Wang's fastest horse.
5. When Tian Ji's fastest horse is equal to Qi Wang's fastest horse, take the slowest horse and Qi Wang's fastest horse.
The correctness of the greedy horse race by Tian Ji is proved.
Let's talk about the simple proof:
1. When Tian Ji's slowest horse is faster than Qi Wang's slowest horse, he wins the first game. It is better to use the most useless horse to win the slowest horse of Qi Wang.
2. When Tian Ji's slowest horse was slower than Qi Wang's, he lost one game with Qi Wang's fastest horse. Because Tianji's slowest horse is always about to lose, it is better to use it to consume Qi Wang's most useful horse.
3. When Tian Ji's slowest horse was equal to Qi Wang's slowest horse's slowness, he discussed it in 4 and 5.
4. When Tian Ji's fastest horse is faster than Qi Wang, he wins the first game. Because the fastest horse is used to win other fast horses, other slow horses can win any horse.
5. when Tian Ji's fastest horse was slower than Qi Wang, he lost the slowest horse and Qi Wang's fastest horse, because he had to lose one, it is better to lose the most useless horse.
6. When Tian Ji's fastest horse is equal to Qi Wang's fastest horse, this will be discussed. The greedy method is to take the slowest horse and Qi Wang's fastest horse.
The previous proof is just like an ordinary principle. Everyone can agree with it at first glance. If there is no objection, I will not elaborate on it.
Proof: When Tian Ji's fastest horse and Qi Wang's fastest horse are equal, he gets the slowest horse and Qi Wang's fastest horse ratio has the optimal solution.
1) Suppose they have n horses. When n = 2.
A1 a2
B1 b2
Because Tianji's fastest horse is equal to Qi Wang's fastest horse, a1 = b1, a2 = b2. Therefore, there are two ways to compete in this situation, and both of them are equally scored.
2) When Series a and Series B are all in phase (a1 = b1, a2 = b2... an = bn). Obviously, the slowest horse and Qi Wang's fastest horse ratio have the optimal solution. They can win n-1 long, lose one game, and cannot find a better solution.
Method.
3) when all the elements of Series a and Series B are equal (a1 = b1 = a2 = b2.... = an = bn), they cannot win or lose.
Now, assuming that there is an optimal solution for getting the slowest horse and Qi Wang's fastest horse ratio when there are n + 1 horses, it is proved that there is also an optimal solution for getting the slowest horse and Qi Wang's fastest horse ratio.
Series
A1 a2 a3 a4... an + 1
B1 b2 b3 b4... bn + 1
Where ai> = ai-1, bi> = bi-1
When not all columns a and B are equal, use the slowest horse to come to the Qi Wang's fastest horse to get the series.
(A1) a2 a3 a4... an + 1
B1 b2 b3 b4... bn (bn + 1)
Discussed in four cases
1. b1 = b2, an = an + 1
Then there is
A2 a3 a4...
B2 b3 b4... bn
Here a2> = a1, a1 = b1, b1 = b2, obtained a2> = b2 (this relationship is not discussed later), an> = bn.
At this time, if a2 = b1, there is an optimal solution based on the induction hypothesis, otherwise a2> there is an optimal solution based on the previous "Principle" argument.
When and only when Series a and all the elements of Series B are equal, an + 1 = b1 is obtained. Therefore, an + 1> b1, win back the slowest horse and the fastest horse to lose Qi Wang.
2. b1 <= b2, an = an + 1
Switch the location of b1 and b2,
Series
(A1) a2 a3 a4... an + 1
B2 b1 b3 b4... bn (bn + 1)
A2> = a1, an> = bn,
For sub-tables
A2 a3 a4...
B1 b3 b4... bn
There is an optimal solution based on the previous "Principle" or induction hypothesis.
An + 1> = b2, when and only when b2 = b3 = b4 = .. when it is = bn + 1, an + 1 = b2. In this case, other elements <= b1, b2, b3, b4 .. bn, for this part, can win x disks (x <= n). If we don't take the slowest horse and Qi Wang's fastest horse, we will take the fastest horse and Qi Wang's fastest horse, at this time flat a disk, can win the X-1 disk, and take the slowest horse to Qi Wang the fastest horse ratio, lose a disk can win x disk, in general, still X this number, no loss.
3. b1 = b2, an <= an + 1
4. b1 <= b2, an <= an + 1 proves that the method is similar and does not overlap.
When it is proved that there are n + 1 horses, when Tian Ji and Qi Wang are at the same speed as the fastest and slowest horse, Qi Wang and Qi have the best solution, it is known that it is true when n = 2, so for n> 2 and is an integer (nonsense, the only number of horses is of course an integer. When n = 1, this does not seem to need to be discussed.
Example:
#include
#include
#include
#includeusing 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
>a[i]; for(int i=0;i
>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[0]) { money++; for(int j=0;j
b[0]) { money++; for(int j=0;j