HDU---1052---tian bogey horse racing

Source: Internet
Author: User
Tags rounds

It's a good feeling to see someone else explain and prove it.Tin bogey Horse racing template problem

Here's how the greedy approach is:

1. When Tian bogey slowest Mabizi the slowest horse fast, win a first

2. When Tian bogey slowest Mabizi slowest horse slow, and king the fastest ponies, lose a

3. When Tian bogey fastest Mabizi the fastest horse fast, win a first.

4. When Tian bogey fastest Mabizi the fastest horse slow, take the slowest horse and King fastest ponies, lose one.

5. When Tian bogey fastest horse and King fastest Minamisoma etc, take the slowest Malay and king the fastest horse ratio.

The correctness of the greedy horse racing is proved.

First of all, the proof of the simple situation: 1. When Tian bogey slowest Mabizi the slowest horse fast, win a first. Because always want to win King the slowest horse, it is better to win it with the most useless Malay.

2. When Tian bogey slowest Mabizi slowest horse slow, and King fastest ponies, lose one. Because Tian Ji the slowest horse always to lose, it is better to use it to consume King most useful horse.

3. When the slowest and king slowest horses are equal, the 4 and 5 are discussed.

4. When Tian bogey fastest Mabizi the fastest horse fast, win a first. Because the fastest horse is used to win others fast horse, others slow horse what horse can win.

5. When Tian bogey fastest Mabizi the fastest horse slow, take the slowest horse and king the fastest ponies, lose a one, because anyway to lose a, rather than the most useless horse lose.

6. When Tian bogey the fastest horse and king the fastest minamisoma and so on, this is going to start the discussion, the greedy method is to take the slowest Malay and king the fastest horse ratio. Before the proof like the axiom, we can see all agree, no objection, will not elaborate.

Proof: Tian bogey fastest horse and King fastest Minamisoma and so on when the slowest Malay and King fastest ponies have the best solution.

1) Suppose they have n horses, when they look at the n=2.

A1 A2 B1 B2

Because Tian bogey the fastest horse and king the fastest minamisoma so a1=b1,a2=b2 so this situation has 2 kinds of game way, easy to get the two ways to score equal.

2) When the sequence A and Series B are all equal (A1=B1,A2=B2...AN=BN), obviously the slowest Malay and King fastest ponies have the best solution, can win n-1 long, lose 1, can't find a better side

The law. 3) When the sequence A and the B elements are all equal (A1=B1=A2=B2...=AN=BN), can not win or lose.

Now suppose that n horses take the slowest Malay and King fastest ponies have the best solution, proving that there are n+1 horses with the slowest Malay and King fastest ponies also have the optimal solution.

Sequence A1 A2 A3 a4...an an+1 B1 B2 b3 b4...bn bn+1

where ai>=ai-1,bi>=bi-1

When sequence A and Series B are not all equal, take the slowest Malay and king the fastest horse-to-be series (A1) A2 A3 a4...an an+1 B1 B2 b3 b4...bn (bn+1)

There are 4 kinds of situation discussion 1.b1=b2,an=an+1 there is A2 A3 a4...an B2 B3 b4...bn which a2>=a1,a1=b1,b1=b2, A2>=B2 (after this size relationship no longer discussed), An>=bn. If A2 at this time =B1, according to the inductive hypothesis, has the optimal solution, otherwise the a2> has the best solution according to the front "axiom" argument. When and only if the a sequence, b sequence elements are all equal when there is AN+1=B1, has been proven, so an+1>b1, win back the slowest Malay and king the fastest horse than the loss of that one.

2.b1<=b2,an=an+1 Exchange B1,B2 position, Sequence number (A1) A2 A3 a4...an an+1 B2 B1 b3 b4...bn (bn+1) at this time a2>=a1,an>=bn, for the child table A2 A3 A4. .. An B1 B3 b4...bn has the optimal solution according to the preceding "axiom" or inductive hypothesis. AN+1>=B2, when and only if b2=b3=b4=. =bn+1 when there is an+1=b2, in this case, the other elements of a <=b1,b2,b3,b4. BN, for this part, can win X (x<=n), if not take the slowest Malay and King fastest ponies to take the fastest Malay and king the fastest ponies, at this time a plate, can win x-1 disk, and take the slowest Malay and king the fastest ponies, lose a disk can win X disk, in general, or x this number, no loss.

3.b1=b2,an<=an+1 4.b1<=b2,an<=an+1 Proof method is similar, no longer repeat.

To be properly n+1 horse, Tian Bogey and king the fastest and slowest horse speed is equal, take the slowest Malay and King fastest ponies have the best solution, known when the n=2 is established, so for n>2 and for the whole (nonsense, the number of horses is of course an integer) is also established. When N=1 .... This does not seem to be discussed.

Problem Descriptionhere 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 higher speed always beat a vertex of 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. Inputthe input consists of up to 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. The input ends with a line is has a single 0 after the last test case. Outputfor each input case, output a line containing a single number, which are the maximum money Tian Ji'll get, in Silve R dollars. Sample Input392 7195 74220 2020 20220 1922-Sample Output20000 Source2004 Asia regional Shanghai
#include <cstdio>#include<cstring>#include<iostream>#include<queue>#include<stack>#include<cmath>#include<map>#include<algorithm>using namespaceStd;map<string,string>Mp;vector<vector<int> >G;Const intinf=0x3f3f3f3f;Const intmaxn=1100;intA[MAXN], B[MAXN];intN, M;intMain () { while(SCANF ("%d", &N), N) { for(intI=1; i<=n; i++) scanf ("%d", &A[i]);  for(intI=1; i<=n; i++) scanf ("%d", &B[i]); Sort (a+1, a+n+1); Sort (b+1, b+n+1); intLA, LB, RA, RB, ans1, Ans2; Ans1=ans2=0; La=lb=1; RA=rb=N;  for(intI=1; i<=n; i++)        {            if(A[ra] >B[RB]) {ans1++; RA--; RB--; }            Else if(A[ra] <B[RB]) {Ans2++; La++; RB--; }            Else if(A[la] >b[lb]) {ans1++; La++; LB++; }            Else            {                if(a[la]!=B[RB]) ans2++; La++; RB--; }} printf ("%d\n", (ANS1-ANS2) * $); }    return 0;}
View Code

Recommendjgshining

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