HDU 1052 Tian Ji -- the horse racing

Source: Internet
Author: User
Tags rounds
Tian Ji -- the horse racing

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 18291 accepted submission (s): 5327


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 input392 83 7195 87 74220 2020 20220 1922

 

Sample output20000 greedy question: Tian Ji horse racing ---- Tian Ji and Qi Wang each have n horses, each of which has to compete, win 200 in a game, lose 200 in a game, q: How can I win the most money for Tian Ji? In order to win the most, Tian Ji should adopt a strategy to circumvent his knowledge. First, sort all horses in the two in the order of size to size, and then adopt the following rules: if Tian Ji's fastest horse is faster than Qi Wang, he will lose the game, and Tian Ji wins the game. If Tian Ji's fastest horse is slower than Qi Wang's fastest horse, in this case, Tianji's slowest horse, PK, Qi Wang's fastest horse, and Tian Ji's one game. Otherwise, if Tian Ji's slowest horse is faster than Qi Wang's slowest horse, the game is defeated, and Tian Ji wins one game; if Tian Ji's slowest horse is slower than Qi Wang's slowest horse, Tian Ji's slowest horse is used to PK Qi Wang's fastest horse, and Tian Ji loses a game; if Tian Ji's fastest horse is equal to the slowest horse and Qi Wang's fastest horse is the slowest horse, then Tian Ji's slowest horse is used to PK Qi Wang's fastest horse.
1 # include <iostream> 2 # include <algorithm> 3 using namespace STD; 4 # define n 1005 5 Int Tian [N], King [N]; 6 7 int CMP (int A, int B) 8 {9 return A> B; 10} 11 int main () 12 {13 int N, I, j; 14 int counts, i1, J1; 15 while (CIN> N & N) 16 {17 for (I = 0; I <n; I ++) 18 CIN> Tian [I]; 19 for (I = 0; I <n; I ++) 20 CIN> King [I]; 21 sort (Tian, tian + N, CMP); 22 sort (king, king + N, CMP); 23 counts = 0; 24 I = I1 = 0; 25 J = J1 = n-1; 26 While (I <= J) 27 {28 If (Tian [I]> King [I1]) // Tian Ji's fastest horse is faster than Qi Wang's. 29 {30 counts ++; 31 I ++; 32 I1 ++; 33} 34 else if (Tian [I] <King [I1]) // Tianji's fastest horse is 35 {36 counts --; 37 J --; 38 I1 ++; 39} 40 else41 {42 if (Tian [J]> King [J1]) // Tian Ji's slowest horse is faster than Qi Wang's. 43 {44 counts ++; 45 J --; 46 J1 --; 47} 48 else if (Tian [J] <King [J1]) // Tian Ji's slowest horse is slower than Qi Wang's slowest horse. 49 {50 counts --; 51 j --; 52 I1 ++; 53} 54 else55 {56 If (Tian [J]! = King [I1]) // the slowest horse in Tianji is not equal to the fastest horse in Qi Wang. 57 counts --; 58 j --; 59 I1 ++; 60} 61} 62} 63 cout <count * 200 <Endl; 64} 65 return 0; 66}
View code

 

 

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.