Tianji horse racing

Source: Internet
Author: User
Tags rounds
Question from: NYOJ Tianji horse racing Time limit: 3000 MS | memory limit: 65535 KB difficulty: 3
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 parameter 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.
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 18
Sample output
20000

There are multiple groups of test data. For each group of test data, enter a number n in the first line, which indicates that n horses are involved in the competition. Then, enter two rows of data, first, Tian Ji's speed of every horse, second, Qi Wang's speed of every horse, and output a line indicating the amount of money that Tian Ji Won (200 at a time, and 200 at a time)

# Include <stdio. h> # include <stdlib. h> int tian [1005], king [1005]; // sort int com (const void * a, const void * B) in ascending order) {return (* (int *) B-* (int *) a);} // read the speed of each horse void read (int n) {int I; for (I = 0; I <n; I ++) {scanf ("% d", & tian [I]) ;}for (I = 0; I <n; I ++) {scanf ("% d", & king [I]) ;}// calculate the number of wins int match (int n) {qsort (tian, n, sizeof (tian [0]), com); qsort (king, n, sizeof (king [0]), com); int tl, tf, kl, kf, win, lose; tl = kl = n-1; tf = kf = 0; win = l Ose = 0; while (tf <= tl) // traverse {if (tian [tf]> king [kf]) {// if tian Ji's fastest horse is faster than Qi Wang's fastest horse, win a game win ++; tf ++; kf ++;} else if (tian [tl]> king [kl]) {// If Tian Ji's slowest horse is faster than Qi Wang's slowest horse, win a win ++; tl --; kl --;} else {if (tian [tl] <king [kf]) // if tian Ji's slowest horse is slower than Qi Wang's fastest horse, lose is a lose ++; tl --; kf ++ ;}}return (win-lose) * 200 ;}int main () {int n; while (scanf ("% d", & n )! = EOF) {read (n); printf ("% d \ n", match (n);} return 0 ;}


Tianji horse racing

Related Article

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.