Hdu1052 Tian Ji -- the horse racing

Source: Internet
Author: User
Tags rounds

Reprinted please indicate the source: http://blog.csdn.net/u012860063

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1052

Tian Ji -- the horse racing Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 17346 accepted submission (s): 5043


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 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.
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
Source2004 Asia Regional Shanghai recommendjgshining | we have carefully selected several similar problems for you: 1051 1257 1053 1789 2187
/*
The fastest horse in Tianji is compared with Qi Wang's fastest horse in three cases.
1. Tian Ji's fastest horse is faster than Qi Wang's, so he directly uses Tian Ji's fastest horse to win Qi Wang's fastest horse.
2. Tian Ji's fastest horse is slower than Qi Wang's, so he uses Tian Ji's slowest horse to lose Qi Wang's fastest horse.
3. Tian Ji's fastest horse is the same as QI Wang's fastest horse.
First, compare the slowest horse in Tianji with the slowest horse in Qi Wang.
If Tian Ji is faster than Qi Wang, he directly wins Qi Wang's slowest horse.
Otherwise, Tian Ji's slowest horse will go to the fastest horse match with Qi Wang.
If the fastest and slowest horses are the same, use Tian Ji's slowest horse and Qi Wang's fastest horse-to-horse ratio.
*/

The Code is as follows:
# Include <cstdio> # include <cmath> # include <cstring> # include <string> # include <cstdlib> # include <climits> # include <ctype. h> # include <queue> # include <stack> # include <vector> # include <deque> # include <set> # include <map> # include <iostream> # include <algorithm> using namespace STD; # define PI ACOs (-1.0) # define INF 0x3fffffffint main () {int N, I, j; int A [1047], B [1047]; while (~ Scanf ("% d", & N) {for (I = 1; I <= N; I ++) {scanf ("% d ", & A [I]);} Sort (a + 1, A + n + 1); for (I = 1; I <= N; I ++) {scanf ("% d", & B [I]);} Sort (B + 1, B + n + 1); int sum = 0; int KA1, kb1; // calculate the currently used serial number boundary int ka2 and kb2 of the horse with a slow speed of Tian Ji and Qi Wang respectively; // calculate the KA1 = kb1 = 1; ka2 = kb2 = N; for (I = N; I> = 1; I --) {if (a [ka2]> B [kb2]) // if Tian Ji's fastest horse is faster than Qi Wang, eat {sum + = 200; ka2 --, kb2 --;} else if (a [ka2] = B [kb2]) // if the horse speed on both sides is the same {if (a [KA1]> B [kb1]) // if Tian Ji's slowest horse is faster than Qi Wang's slowest horse, directly eat {sum + = 200; KA1 ++, kb1 ++ ;} else if (a [KA1] <= B [kb1]) // if Tian Ji's slowest horse and Qi Wang's slowest horse speed are the same or slower than Qi Wang's {if (a [KA1] <B [kb2]) // if Tian Ji's slowest horse is slower than Qi Wang's fastest horse, use the slowest one to lose Qi Wang's fastest {// PS: readers may vomit, the slowest case is slower than the fastest case. For example, the second case of the question is sum-= 200; KA1 ++; kb2 --;} else if (a [KA1] = B [kb2]) // The horses on both sides are at the same speed, so you can directly ignore the change of money loss {KA1 ++, kb2 -- ;}} else if (a [ka2] <B [kb2]) // if Tian Ji's fastest horse is slower than Qi Wang's fastest horse, use Tian Ji's slowest horse to lose Qi Wang's fastest horse {sum-= 200; KA1 ++; kb2 -- ;}} printf ("% d \ n", sum );} return 0 ;}



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.