1052 Tian Ji -- The Horse Racing

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?”

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

1
2
3
4
5
6
7
8
9
10
3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
0

Sample Output

1
2
3
200
0
0

Suggest Answer

实现利益最大化,其实就是贪心算法,做好排序就好了,比较简单的一道题,但是花了很长时间才写出来,主要因为是赛马的逻辑有点搞不懂

先拿田忌和国王最快的马比,如果田忌的更快,那就比赛,赢一局

如果田忌最快的马要比国王的慢,那就拿田忌最慢的马和国王最快的马比,输一局

如果田忌和国王最快的马速度相等,就拿田忌和国王最慢的马比,如果田忌的最慢马比国王的要快,就比赛,赢一局

如果田忌的最慢马等于或小于国王的最慢马,那就拿田忌的最慢马和国王的最快马比赛,输一局

上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.*;

public class _1052 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
myComparator c = new myComparator();
while (input.hasNextInt()) {
int n = input.nextInt();
int win = 0;
if (n == 0) {
break;
}
Integer[] tian = new Integer[n];
Integer[] king = new Integer[n];
int raced = 0;
for (int i = 0; i < n; i++) {
tian[i] = input.nextInt();
}
for (int i = 0; i < n; i++) {
king[i] = input.nextInt();
}
Arrays.sort(tian, c);
Arrays.sort(king, c);
while (tian[0] != 0) {
if (tian[0] > king[0]) {
tian[0] = 0;
king[0] = 0;
win++;
raced++;
} else if (tian[0] < king[0]) {
tian[n - raced - 1] = 0;
king[0] = 0;
win--;
raced++;
} else {
if (tian[n - raced - 1] > king[n - raced - 1]) {
tian[n - raced - 1] = 0;
king[n - raced - 1] = 0;
win++;
raced++;
} else {
if (tian[n - raced - 1] < king[0])
win--;
tian[n - raced - 1] = 0;
king[0] = 0;
raced++;
}
}
Arrays.sort(tian, c);
Arrays.sort(king, c);
}
System.out.println(win * 200);
input.close();
}
}
}

class myComparator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
if (o1 < o2) {
return 1;
} else if (o1 > o2) {
return -1;
} else {
return 0;
}
}
}