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
3 92 83 71 95 87 74 2 20 20 20 20 2 20 19 22 18 0
Sample Output
200 0 0
Source
2004 Asia Regional Shanghai
#include "stdio.h"
int main() {
int n, a[1000], b[1000];
while (scanf("%d", &n) != EOF && n != 0) {
int i, j, temp;
int sum = 0;
for (i = 0; i < n; i++) { //田忌马
scanf("%d", &a[i]);
}
for (i = 0; i < n - 1; i++) { //降序排列
for (j = 0; j < n - 1 - i; j++) {
if (a[j] < a[j+1]) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (i = 0; i < n; i++) { //齐王马
scanf("%d", &b[i]);
}
for (i = 0; i < n - 1; i++) { //降序排列
for (j = 0; j < n - 1 - i; j++) {
if (b[j] < b[j+1]) {
temp = b[j];
b[j] = b[j+1];
b[j+1] = temp;
}
}
}
i = j = 0;
int t = n - 1;
int k = n - 1;
int c;
for (c = 0; c < n; c++) {
if (a[i] > b[j]) { //田忌的快马快于齐王,用田忌的优势马在赢得比赛的同时消耗齐王的快马
sum += 200;
i++;
j++;
} else if (a[i] < b[j]) { //齐王的快马快,用田忌的慢马比
sum -= 200;
j++;
t--; //消耗田忌的慢马
} else if (a[i] == b[j]) { //两人马一样快时
if (a[t] > b[k]) { //田忌的慢马快于齐王,最慢的都能赢,稳赚
t--; //都消耗 最慢马
k--;
sum += 200;
} else { //在田忌最慢马的劣势下,去消耗齐王的快马
if (a[t] < b[j]) {
sum -= 200;
} else if (a[t] > b[j]) { //田忌的慢马都能赢,岂不美哉
sum += 200;
}
j++;
t--;
}
}
}
printf("%d\n", sum);
}
return 0;
}
3
92 83 71
95 87 74
200
2
20 20
20 20
0
2
20 19
22 18
0
0
--------------------------------
Process exited after 1.548 seconds with return value 0
请按任意键继续. . .
题意:田忌赛马
算法:贪心算法(寻找最优解)
先从 快马 进行比较:
田忌快马速度 快 时,则用这个快马去消耗齐王的优势马(让齐王的马越来越差)
田忌快马速度 慢 时,比不过,就用最垃圾的马的去消耗齐王的优势马
两人马速度 相同 时,开始从比较 慢马 比较:
田忌慢马 快于 齐王慢马,最差的马能赢则赢,稳赚不赔
田忌慢马 慢 或 等于 齐王慢马,比不过,我就去消耗你的优势马,给我的优势马创造机会