Poj-2287-Tian Ji -- the horse racing

Source: Internet
Author: User
Tags rounds

This question is very understandable, that is, how much can Tian Ji win at most, win 200 gold coins in a game, lose 200 gold coins in a game, and lose gold coins in a draw!

Idea: it is obvious that you can use this question.GreedyDo it! If you are greedy, you will be greedy. If you cannot win, you will be sure not to lose. If you lose, you will also make contributions to the subsequent competitions, in this game, you can lose and have no regrets-that is, you can use your worst horse to compete with the king's strongest horse "!

The Code contains detailed questions and ideas. I think it should be easy to understand!

The Code is as follows:


# Include <algorithm> # include <stdio. h> # include <string. h> using namespace STD; int main () {int N, Tian [5203], King [5204]; while (scanf ("% d", & N )! = EOF & N) {for (INT I = 0; I <n; I ++) scanf ("% d", & Tian [I]); for (INT I = 0; I <n; I ++) scanf ("% d", & King [I]); sort (Tian, Tian + n ); sort (king, king + n); int maxt = n-1; int mint = 0; int maxk = n-1; int mink = 0; int ans = 0; while (maxt> = mint) {// printf ("Ans = % d \ n", ANS); If (Tian [maxt]> King [maxk]) {// Tianji's strongest horse can win King's strongest horse! Maxt --; maxk --; ans ++;} else if (Tian [mint]> King [mink]) {// Tianji's weakest horse can win King's weakest horse! Mint ++; mink ++; ans ++;} else {// the current situation is that none of the strongest and weakest horses in Tianji can win .. // King's strongest and weakest horses! // You need to change the value to a small one! If (Tian [mint] <King [maxk]) ans --; // in this case, you must lose one! Otherwise, it would be a draw, and ANS would not join! Mint ++; maxk --;} // printf ("Tian Ji maxt = % d; mint = % d \ n", maxt, mint ); // printf ("Qi Wei maxk = % d; mink = % d \ n", maxk, mink);} printf ("% d \ n", ANS * 200 );} return 0 ;}




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

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 180

Sample output

20000

Poj-2287-Tian Ji -- the 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.