Description
Here 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.
Input
The 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.
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
392 83 7195 87 74220 2020 20220 1922 180
Sample Output
20000
This is a greedy algorithm, the first is to sort (from big to small), and then to compare. 2 kinds of 1, when the fastest two horse speed is not the same, better solve, skip. 2, when the fastest two horses at the same speed, see not run the slowest two horses, if the field bogey fast, then this two direct comparison, and then carry out the above work, until the field to find out the horse is not king, let this horse and King the fastest horse to compare.
1#include <stdio.h>2 inttian[1010],q[1010];3 voidPaixu (intA[],intN)4 {5 intI=0, j=n-1;6 intt=a[0];7 if(n>1){8 while(i<j)9 {Ten for(; i<j;j--) One if(a[j]>t) { Aa[i++]=A[j]; - Break; - } the for(; i<j;i++) - { - if(a[i]<t) { -a[j--]=A[i]; + Break; - } + } A } ata[i]=T; - Paixu (a,i); -Paixu (a+i+1, n-i-1); - } - } - voidFunintT[],intQ[],intN) in { - intI=0, g=n-1, k=n-1, h=0, sum=0; to for(; i<=k;i++) + { - if(q[i]<T[h]) { thesum=sum+ $; *h++; $ }Panax Notoginseng Else if(q[i]>T[h]) - { thesum=sum- $; +g--; A } the + Else { - for(; T[g]>q[k];g--, k--) $sum=sum+ $; $ if(q[i]>T[g]) - { -sum=sum- $; theg--; - }Wuyi Else { theg--; - } Wu } - } Aboutprintf"%d\n", sum); $ } - intMain () - { - intI,n; A while(SCANF ("%d", &n)!=eof&&N) + { the for(i=0; i<n;i++) -scanf"%d",&tian[i]); $ for(i=0; i<n;i++) thescanf"%d",&q[i]); the Paixu (tian,n); the Paixu (q,n); the Fun (tian,q,n); - } in return 0; the}
D-tian Ji--The Horse Racing (Tian Bogey horse racing problem)