"Strategy" UVa 1344-tian Ji-The Horse Racing (Tian Bogey horse racing)

Source: Internet
Author: User

Here is a famous stories in Chinese.

That is 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 has 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.
Being the most powerful mans in the country, the king have 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?

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 higher speed always beat a vertex for 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 up to test cases. Each case starts with a positive integer n ( n+) on the first line, which is the number of HO RSes 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. The input ends with a line this has a single '0' after the last Test case.

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  95 87 74 2 20 20 20 20 2 20 19 22 18 0
Sample Output
200 0 0

Greedy strategy:

First, when Tian bogey the fastest ponies king of the fastest horse fast, with Tian bogey fastest horse win King the fastest horse.
Second, when Tian bogey the fastest ponies king of the fastest horse slow, with Tian bogey the slowest horse to the King's fastest horse.
Third, when Tian bogey the fastest horse with the king's fastest horse as fast as the situation. (For the situation three, we should start with the slowest horse to consider)

1, when Tian bogey the slowest ponies king of the slowest horse fast, then use Tian bogey the slowest horse to win the King's slowest horse

2, when Tian bogey slowest ponies king slowest horse slow, then use Tian bogey the slowest horse to the king's fastest horse

3, when Tian bogey the slowest horse and king of the slowest minamisoma, and so on, with the most slow horse with the king of the fastest ponies (at this time the King's fastest horse can only be greater than the same field bogey the slowest horse, if greater than, then the field bogey lose, equal, draw)

1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <algorithm>5 using namespacestd;6 Const intMAXN =1010;7 intT[MAXN], K[MAXN];8 intMain ()9 {Ten     intN; One      while(~SCANF ("%d", &n) &&N) A     { -          for(inti =0; I < n; i++) -         { thescanf"%d", &t[i]); -         } -          for(inti =0; I < n; i++) -         { +scanf"%d", &k[i]); -         } +Sort (t, t+n); Sort (k, + Kn); A         intTmax = n1, Kmax = n1;//The number of the fastest horses in T and K at         intTmin =0, kmin =0;//The number of the slowest horses in T and K -         intWin =0, lose =0; -          while(tmin <= Tmax && kmin <=Kmax) -         { -             if(T[tmax] >K[kmax]) -             { intmax--; kmax--; -win++; to             } +             Else if(T[tmax] <K[kmax]) -             { thetmin++; kmax--; *lose++; $             }Panax Notoginseng             Else -             { the                 if(T[tmin] >K[kmin]) +                 { Atmin++; kmin++; thewin++; +                 } -                 Else if(T[tmin] <K[kmin]) $                 { $tmin++; kmax--; -lose++; -                 } the                 Else -                 {Wuyi                     if(T[tmin] <K[kmax]) thelose++; -tmin++; kmax--; Wu                 } -             } About         } $printf"%d\n", ((win-lose) * $)); -     } -     return 0; -}
View Code

"Strategy" UVa 1344-tian Ji-The Horse Racing (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.