HDU 1052 Tian Ji -- The Horse Racing

Tian Ji -- The Horse Racing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8562    Accepted Submission(s): 2380


Problem 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?"

HDU 1052 Tian Ji -- The Horse Racing

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 many 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
3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18 0
 

 

Sample Output
200
0
0
 
 
 
 
自己开始想的是后,虽然知道是贪心,但是只从要输的那边从最快到最慢,来考虑。
 
 
这题是这样的:设要赢那方为A队,输的为B队。
A最快 > B最快:即A的最快能打败B的所有队员,为了后面着想,肯定跟B的最快比。
A最快 < B最快:即B的最快能打败A的所有队员,为了B对后面的输,所以B队最快那个就要浪费掉,拿去打A队最慢的。
A最慢 > B最慢:即B的最慢必定会输,A随便拿一个都赢他,当然拿最差的来赢他啦。
A最慢 < B最慢:即A的最慢必定会输,B随便拿一个都赢他,要B队浪费点,所以B队拿最快的和他比。
A最快 == B最快 && A最慢 == B最慢,最优是拿A最慢和B最快打,这样的话中间A队都能赢B队。
 
 
 
View Code
 1 #include<stdio.h>
2 #include<stdlib.h>
3 #define maxn 1100
4 int a[maxn],b[maxn];
5 int cmp(const void* a,const void* b)
6 {
7 return *(int*)b - *(int*)a;
8 }
9 int main()
10 {
11 int n,i;
12 int first_a,first_b,end_a,end_b,count;
13 while(scanf("%d",&n) == 1 && n)
14 {
15 for(i = 0;i < n;i++)
16 {
17 scanf("%d",&a[i]);
18 }
19 for(i = 0;i < n;i++)
20 {
21 scanf("%d",&b[i]);
22 }
23 //快排
24 qsort(b,n,sizeof(b[0]),cmp);
25 qsort(a,n,sizeof(a[0]),cmp);
26 count = 0;
27 first_a = first_b = 0;
28 end_a = end_b = n - 1;
29 for(i = 0;i < n;i++)
30 {
31 if (a[first_a] > b[first_b])//最快赢
32 {
33 first_a++;
34 first_b++;
35 count++;
36 }
37 else if (a[first_a] < b[first_b])//最快输
38 {
39 first_b++;
40 end_a--;
41 count--;
42 }
43 else
44 {
45 if(a[end_a] < b[end_b])//最慢输
46 {
47 first_b++;
48 end_a--;
49 count--;
50 }
51 else
52 {
53 if(a[end_a] > b[end_b])//最慢赢
54 {
55 end_a--;
56 end_b--;
57 count++;
58 }
59 else
60 {
61 if(a[end_a] < b[first_b])//同时相等时,A队最慢对B队最快。
62 {
63 end_a--;
64 first_b++;
65 count--;
66 }
67 }
68 }
69 }
70 }
71 printf("%d\n",200 * count);
72 }
73 return 0;
74 }
 
 

你可能感兴趣的:(HDU)