Hdu-1052-tian Ji-The Horse Racing (c + + && simple greedy)

Source: Internet
Author: User
Tags rounds

Tian Ji--The Horse RacingTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 21782 Accepted Submission (s): 6374


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 Input
392 83 7195 87 74220 2020 20220 1922 180

Sample Output
20000

Source2004 Asia Regional Shanghai
recommendjgshining | We have carefully selected several similar problems for you:1009 1045 1800 1257 1053

Thank you: Talent _wdnlwzd provide ideas and analysis:
Greedy algorithm (here is the list of all the best solutions): Every time the fastest horse to take the field bogey and king the fastest horse compared, there are three kinds of situations.
First, Tian bogey fastest Mabizi fastest, then directly with the fastest horse to win King the fastest horse.
Second, Tian bogey the fastest Mabizi the fastest slow, then use the most slow horse to lose King the fastest horse.
Third, Tian bogey the fastest horse and king the fastest horse speed.
The slowest horse is compared with King's slowest horse.
If Tian bogey Bitziwan fast, directly win King the slowest horse.
Otherwise, the slowest horse to go again with King the fastest horse compared if the fastest and slowest horses are the same, then ignore the change in money.


#include <iostream> #include <cstdio> #include <algorithm>using namespace Std;int main () {int n,i,j;    int a[1047],b[1047];        while (~SCANF ("%d", &n) && N) {for (i=1;i<=n;i++) {scanf ("%d", &a[i]);        } sort (a+1,a+1+n);//field bogey horse speed sort for (i=1;i<=n;i++) {scanf ("%d", &b[i]);        } sort (b+1,b+1+n);//King horse speed sort int sum=0;        The int t1,k1;//calculates the field bogey and the King slow Horse is not used the current ordinal boundary int t2,k2;//respectively calculates the field bogey and the King slow Horse has not used the current ordinal boundary t1=k1=1;        T2=k2=n;                for (i=n;i>=1;i--) {if (A[T2]&GT;B[K2])//If Tian bogey the fastest Mabizi fastest horse directly eat {sum+=200;                t2--;            k2--;                } else if (A[T2]&LT;B[K2])//If the fastest Mabizi the fastest horse slow, with Tian bogey the slowest horse to lose King the fastest horse {sum-=200;                t1++;            k2--; } else if (A[T2]==B[K2])//If both the fastest horse speed is the same {if (A[T1]&GT;B[K1])//If Tian bogeyThe slowest Mabizi the slowest fast, eats directly {sum+=200;                    t1++;                k1++; } else if (A[T1]&LT;=B[K1])//If the slowest Mabizi slowest horse slow or the same speed {if (A[T1]&LT;B[K2])//                        If Tian bogey slowest Mabizi the fastest horse slow, with the slowest to lose king fastest {//ps. Here Tian bogey the slowest horse is not necessarily Bitziwan the fastest horse slow, there may be equal, the topic of the second case sum-=200;                        t1++;                    k2--;                         } else if (A[T1]==B[K2])//Both horses are all the same speed situation, then directly can ignore the change of money {t1++;                    k2--;    }}}} printf ("%d\n", sum); } return 0;}


Attached author Address: http://blog.csdn.net/u012860063/article/details/34103409

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdu-1052-tian Ji-The Horse Racing (c + + && simple greedy)

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.