题目
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
题解
知识点:贪心,区间dp。
首先是一个贪心的结论,双方从大到小排列,按qiwang马从大到小比,tianji比不过就用自己最小的马,比得过就用最大的马。
因为如果tianji最大的马都不能比过的qiwang最大的马时,肯定拿当前最小的马比;如果能比过,那么直接比就行,不需要找到最小能比过的马,因为能比过对方最大的马那意味着后面的马都能比过,取谁都一样。
随后就是个区间dp,设 \(dp[i][j]\) 为用区间 \([i,j]\) 的马比qiwang \([n-(j-i),n]\) 的马,赢一次加一,输一次减一,平均不动。
时间复杂度 \(O(n^2)\)
空间复杂度 \(O(n^2)\)
代码
#include <iostream>
#include <algorithm>
using namespace std;
int a[1007], b[1007], dp[1007][1007];
int cmp(int i, int j) {
return a[i] == b[j] ? 0 : a[i] < b[j] ? -1 : 1;
}
bool ccmp(int a, int b) {
return a > b;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
while (cin >> n, n) {
for (int i = 1;i <= n;i++) cin >> a[i];
for (int i = 1;i <= n;i++) cin >> b[i];
sort(a + 1, a + n + 1, ccmp);
sort(b + 1, b + n + 1, ccmp);
for (int i = 1;i <= n;i++) dp[i][i] = cmp(i, n);
for (int l = 2;l <= n;l++) {
for (int i = 1;i <= n - l + 1;i++) {
int j = i + l - 1;
dp[i][j] = max(dp[i + 1][j] + cmp(i, n - l + 1), dp[i][j - 1] + cmp(j, n - l + 1));
}
}
cout << dp[1][n] * 200 << '\n';
}
return 0;
}
标签:horses,king,Horse,POJ2287,--,Tian,int,Ji,problem
From: https://www.cnblogs.com/BlankYang/p/16589770.html