Hdu1052 Tian Ji -- the horse racing greedy

Source: Internet
Author: User
Tags rounds
Tian Ji -- the horse racing Time limit:1000 ms Memory limit:32768kb 64bit Io format:% I64d & % i64usubmit status practice HDU 1052

Description

Here 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 horse racing problem can be simply viewed 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 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

 392 83 7195 87 74220 2020 20220 1922 180 
 

Sample output

 20000 
 

No one in Tianji and Qi Wang has n horses. They won 200 yuan in a game and how to make sure that Tianji can win the most.

Greedy Algorithm. The following is the analysis and proof of a great god. I will only record it later.

The algorithm can use DP, or assign permissions to the connections of each horse to achieve the optimal matching of a bipartite graph. In addition, the algorithm is greedy.
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 and lose one game, so they cannot find a better 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.

The Code contains my own annotations, which should be clear:

# Include <stdio. h> # include <string. h> # include <math. h ># include <algorithm> using namespace STD; int main () {int I, j, ANS, TT, KK, N, t [1100], K [1100]; int CNT; while (scanf ("% d", & N), n) {for (I = 0; I <n; I ++) scanf ("% d ", & T [I]); for (I = 0; I <n; I ++) scanf ("% d", & K [I]); sort (t, T + n); // sort (K, K + n); ans = 0; I = J = 0; // I and J point to Tian Ji and Qi Wang's slowest horse TT = KK = n-1; // TT and KK point to Tian Ji and Qi Wang's fastest horse CNT = 0; // used for counting to ensure loop exit while (CNT! = N) {If (T [I]> K [J]) // (1) if Tian Ji's slowest horse is faster than Qi Wang's slowest horse, he uses the slowest horse, win one first {ans ++; I ++; j ++;} else if (T [I] <K [J]) // (2) if Tian Ji's slowest horse is slower than Qi Wang's slowest horse, it depends on the situation. {If (T [TT]> K [Kk]) // 1. at this time, if Tian Ji's fastest horse is faster than Qi Wang, he will use the fastest horse to win {ans ++; TT --; kk --;} else if (T [TT] = K [Kk]) // 2. if their fastest horses are equal, look at the situations where Tian Ji's slowest horse and Qi Wang's fastest horse (there are two types, equal or slower than Qi Wang's) {If (T [I] = K [Kk]) // ① if the slowest horse in Tianji is equal to the fastest horse in Qi Wang at this time, it is equal to {I ++; kk --;} else // ② otherwise, Tian Ji's slowest horse will be slower than Qi Wang's fastest horse, lose one {// the fastest horse in Tianji is faster than that in Qi Wang. ans --; I ++; kk -- ;}} else // 3. tian Ji's fastest horse is slower than Qi Wang's fastest horse. At this time, he needs to use Tian Ji's slowest horse to consume Qi Wang's fastest horse and lose one {ans --; I ++; kk --;}} else if (T [I] = K [J]) // (3) if their slowest horses are equal, consider their fastest horse situation .. {If (T [TT]> K [Kk]) // 1. tian Ji is quick, win a game {ans ++; TT --; kk --;} else if (T [TT] <K [Kk]) // 2. tian Ji is slow. At this time, he can use Tian Ji's slowest horse to consume Qi Wang's fastest horse and lose one {ans --; I ++; kk --;} else if (T [TT] = K [Kk]) // 3. at this time, the slowest horse and the fastest horse are equal. At this time, the slowest horse than Tian Ji and Qi Wang's fastest horse {If (T [I] <K [Kk]) // ① if Tian Ji's slowest horse is slower than Qi Wang's fastest horse, lose one {ans --; I ++; kk --;} else if (T [I] = K [Kk]) // ② the slowest horse in Tianji is the same as that in Qi Wang's horse, and the horse is equal to {I ++; kk --;}}} CNT ++;} printf ("% d \ n", ANS * 200);} return 0 ;}


Hdu1052 Tian Ji -- the horse racing greedy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.