Nyoj 364 Tianji horse racing

Source: Internet
Author: User
Tags rounds

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 1922 18
Sample output
20000

Tian Ji's horse racing, no explanation... All Chinese

Algorithm analysis:

At first, I thought about comparing them directly from the starting point, but it was complicated to consider when two numbers are the same or even multiple successive identical ones. For the same two horses, comparison is not the key to thinking about the problem.

Two special cases, for example, 95 90 88 76 68 and 99 95 90 88 60 and 95 95 90 80 and 99 95 95 95 95, are true, But wa is tested, I am sure I am not fully aware of it. Please switch your mind to the following:

1. When Tian Ji's fastest horse is faster than Qi Wang's fastest horse, use Tian Ji's fastest horse to fight Qi Wang's fastest horse and win a game.

2. When Tian Ji's slowest horse was faster than Qi Wang's slowest horse, he used Tian Ji's slowest horse to fight Qi Wang's slowest horse and win a game.

3. When neither 1 nor 2 is satisfied, use Tian Ji's slowest horse to fight Qi Wang's fastest horse. If Tian Ji's slowest horse is slower than Qi Wang's fastest, he will lose one game. Otherwise, the draw will be reached.

Note:

If (A [tr] <B [Kl]) // remove the case where all four adjacent numbers are equal ans --;

Complete code:

# Include <iostream> # include <algorithm> # include <functional> using namespace STD; const int maxn = 1000; int A [maxn + 5], B [maxn + 5]; int match (int n) {int TL = 0, TR = n-1, KL = 0, Kr = n-1, ANS = 0; while (TL <= TR) {if (a [TL]> B [Kl]) {ans ++; TL ++; KL ++;} else if (a [tr]> B [Kr]) {ans ++; tr --; KR --;} else {if (a [tr] <B [Kl]) // remove the case where all four adjacent numbers are equal ans --; tr --; KL ++ ;}} return ans * 200;} int main () {int N, I; while (CIN >> N) {for (I = 0; I <n; I ++) CIN> A [I]; for (I = 0; I <n; I ++) CIN> B [I]; sort (A, A + N, greater <int> (); sort (B, B + N, greater <int> (); cout <match (N) <Endl;} return 0 ;}

Nyoj 364 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.