Hdoj 1052 Tian Ji -- The Horse Racing [Tianji Horse Racing] [greedy]

Source: Internet
Author: User
Tags class operator rounds

Idea: first sort by size, and then start from the fastest ratio (if I, j is the slowest end, flag1, flag2 is the fastest end ), the shortest value of Tian is greater than that of King. If Tian is equal to and then inferred, there are three situations:

1. If the value is greater than then, the second-class operator assumes that the slowest field is slower than King's, and the third is smaller than King's fastest comparison;

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


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
 
# Include <stdio. h ># include <algorithm> Using STD: sort; int CMP (int A, int B) {if (a <B) Return true; return false;} int main () {int T [1005], K [1005], n, I, j, C; while (scanf ("% d", & N), n) {c = 0; for (I = 0; I <n; I ++) scanf ("% d", & T [I]); sort (t, t + N, CMP ); for (I = 0; I <n; I ++) scanf ("% d", & K [I]); sort (K, K + N, CMP ); I = J = 0; int flag1 = n-1, flag2 = n-1; // flag1 is Tian's while (I <= flag1) {If (T [flag1]> K [flag2]) {++ C; -- flag1; -- flag2;} else if (T [flag1] = K [flag2]) {If (T [I]> K [J]) {++ C; ++ I; ++ J ;} else if (T [I] = K [J]) {If (T [I] <K [flag2]) -- C; // note that this area is wa several times ++ I; -- flag2;} else if (T [I] <K [J]) {-- C; ++ I; -- flag2 ;}} else {-- C; ++ I; -- flag2 ;}} printf ("% d \ n", C * 200 );}}



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.