首先明确田忌每次出马不是最快就是最慢,dp[i][j]为田忌第i到第j匹马和齐王比赛的结果,dp[i][j]=max(dp[i+1][j]+judge(i,k),dp[i][j-1]+judge(j,k)),枚举齐王出了前k匹马。
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; int dp[1002][1002],tian[1002],king[1002]; int judge(int a,int b) { if(tian[a]>king[b]) return 1; else if(tian[a]<king[b]) return -1; else return 0; } int main() { int n,i,k; while(scanf("%d",&n)!=EOF&&n) { for(i=1;i<=n;i++) scanf("%d",&tian[i]); for(i=1;i<=n;i++) scanf("%d",&king[i]); memset(dp,0,sizeof(dp)); sort(king+1,king+n+1); sort(tian+1,tian+n+1); for(i=1;i<=n;i++) dp[i][i]=judge(i,1); for(k=2;k<=n;k++) for(i=1;i<=n+1-k;i++) dp[i][i+k-1]=max(dp[i+1][i+k-1]+judge(i,k),dp[i][i+k-2]+judge(i+k-1,k)); printf("%d\n",200*dp[1][n]); } }