HDOJ-1052 Tian Bogey horse racing

Source: Internet
Author: User
Tags rounds

Tian Bogey horse racing

Time limit: Ms | Memory Limit: 65535 KB
Difficulty: 3
Describe:

Here is a famous stories in Chinese.

"That's about 2300 years ago." General Tian Ji is a high official in the country Qi. He likes to play horse racing with the king and others. "

"Both of Tian and the king had three horses in different classes, namely, regular, plus, and super. The rule is to has three rounds in a match; Each of the horses must is used in one round. The winner of a single round takes and hundred silver dollars from the loser. "

"The Being the most powerful man in the country, the king had so nice horses, and each class he horse is better than Tian ' S. As a result, each time the king takes six hundred silver dollars from Tian. "

"Tian Ji is not happy on 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 of hundred silver dollars and such a grace in the next match. "

"It is a rather simple trick. Using the He regular class horse race against the Super class from the king, they would certainly lose that round. But then he plus beat the King ' s regular, and his super beat the King's Plus. What's a simple trick. And how does your think of Tian Ji, the high ranked official in China? "

were Tian Ji lives in nowadays, he'll certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he could discover that the horse racing problem can be simply Viewe D 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 a edge between them, meaning we wish to establish this Pair. Then, the problem of winning as many rounds as possible are just to find the maximum matching in this graph. If There is 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 the higher speed always beat a vertex in lower speed. In this case, the weighted bipartite matching algorithm are a too advanced tool to deal with the problem.

In this problem, you is asked to the write a program to solve this special case of matching problem.
Input
The input consists of many test cases. Each case starts with a positive an integer n (n <=) on the first line, which are the number of horses on each side. The next n integers on the second line is the speeds of Tian ' s horses. Then the next n integers in the third line is the speeds of the King ' s horses.
Output
For each input case, output a line containing a single number, which are the maximum money Tian Ji'll get, in Silver doll Ars.

Sample input

3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18

Sample Output

200
0
0

Greedy strategy:

If you are the weakest > King of Tian Bogey:
Tian bogey the weakest PK king The weakest

If you are the weakest < King of Tian Bogey:
Tian bogey the weakest PK King the strongest

If field bogey weakest = King weakest
If Tian bogey strongest > King most strong field bogey the strongest PK King the strongest
Otherwise: Tian bogey the weakest PK King the strongest
}

1#include <stdio.h>2#include <stdlib.h>3 intCompareConst void* P1,Const void*p2) {4     return*(int*) P2-* (int*) P1;5 }6 intMainvoid)7 {8     intN;9      while(~SCANF ("%d", &n) &&N) {Ten         inttian[ +]; One         intking[ +]; A          for(inti =0; I < n; i++) { -scanf"%d", &tian[i]); -         } theQsort (Tian, N,sizeof(int), compare); -          for(inti =0; I < n; i++) { -scanf"%d", &king[i]); -         } +Qsort (King, N,sizeof(int), compare); -         if(tian[0] < King[n-1]) { +printf"%d\n", -( $*n)); A             Continue; at         } -         Else if(Tian[n-1] > king[0]) { -printf"%d\n", $*n); -             Continue; -         } -         intmin_t = N-1, Min_k = n-1; in         intmax_t =0, Max_k =0; -         intWin =0; to          while(n--) { +             if(Tian[min_t] >King[min_k]) { -                 //field weak vs Wang Weak thewin++; *min_t--; $min_k--;Panax Notoginseng                 Continue; -             } the             Else if(Tian[min_t] <King[min_k]) { +                 //field Weak vs Wang Qiang A                 if(tian[min_t]! =King[max_k]) thewin--; +min_t--; -max_k++; $                 Continue; $             } -             Else { -                 if(tian[max_t] >King[max_k]) { the                     //Tian Qiang vs Wang Qiang -win++;Wuyimax_t++; themax_k++; -                     Continue; Wu                 } -                 Else { About                     //field Weak vs Wang Qiang $                     if(tian[min_t]! =King[max_k]) -win--; -min_t--; -max_k++; A                     Continue; +                 } the             } -         } $printf"%d\n", Win * $); the     } the}

HDOJ-1052 Tian Bogey horse racing

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.