题目
时限:1000ms 内存限制:10000K 总时限:3000ms
描述:
田忌与齐王赛马,双方各有n匹马参赛(n<=100),每场比赛赌注为1两黄金,现已知齐王与田忌的每匹马的速度,并且齐王肯定是按马的速度从快到慢出场,现要你写一个程序帮助田忌计算他最好的结果是赢多少两黄金(输用负数表示)。
Tian Ji and the king play horse racing, both sides have n horse (n is no more the 100), every game a bet of 1 gold, now known king and Tian Ji each horse's speed, and the king is definitely on the horse speed from fast to slow, we want you to write a program to help Tian Ji his best result is win the number gold (lost express with the negative number).
输入:
多个测例。
每个测例三行:第一行一个整数n,表示双方各有n匹马;第二行n个整数分别表示田忌的n匹马的速度;第三行n个整数分别表示齐王的n匹马的速度。
n=0表示输入结束。
A plurality of test cases.
Each test case of three lines: the first line contains an integer n, said the two sides each have n horse; second lines of N integers n Tian Ji horse speed; third lines of N integers King n horse speed.
N = 0 indicates the end of input.
输出:
每行一个整数,田忌最多能赢多少两黄金。
how many gold the tian ji win
输入样例:
3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
3
20 20 10
20 20 10
0
输出样例:
1
0
0
0
思路:
状态:dp[i][j] 表示第i场比赛是田忌从后往前用了j匹马。
转移方程:
s(i,j)表示田忌用第i匹马时,齐王用第j匹马时的得分
dp[i - 1][j]表示比赛进行了i- 1场,田忌用了j匹慢马时,所获得的最大收益。为了得到dp[i][j],田忌只能使用前面的第i- j匹快马与齐王的 第j匹马比较。
dp[i - 1][j - 1]表示比赛进行了i-1场,田忌用了j - 1 匹慢马时,所获得的最大利益。为了得到dp[i][j] ,田忌必须用前边的第j匹慢马(即第 n - j + 1匹快马),对战齐王的第j匹快马。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int Tianji[105],Qiwang[105];
int s(int i,int j)
{
if(Tianji[i] > Qiwang[j]) return 1;
else if(Tianji[i] < Qiwang[j]) return -1;
else return 0;
}
int main()
{
int n;
while(cin >> n, n)
{
int dp[105][105];
for(int i = 1;i <= n;i ++)
cin >> Tianji[i];
for(int i = 1;i <= n;i ++)
cin >> Qiwang[i];
sort(Tianji + 1,Tianji + n + 1,greater<int>());
sort(Qiwang + 1,Qiwang + n + 1,greater<int>());
memset(dp,-100,sizeof dp);
dp[0][0] = 0;
for(int i = 1;i <= n;i ++)
{
for(int j = 0;j <= n;j ++)
{
if(j == 0)
dp[i][j] = max(dp[i][j],dp[i - 1][j] + s(i - j,i));
else
dp[i][j] = max(dp[i - 1][j] + s(i - j,i),dp[i - 1][j - 1] + s(n - j + 1, i));
}
}
int maxn = dp[n][0];
for(int i = 1;i <= n;i ++)
{
maxn = max(maxn,dp[n][i]);
}
cout << maxn << endl;
}
return 0;
}
/*
3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
3
20 20 10
20 20 10
*/