没什么好想的,就是一个记忆化搜索。我第一次居然想爆搜~~~

View Code
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int a[1005];
 6 int dp[1005][1005];
 7 int dfs(int left,int right)
 8 {
 9     if(dp[left][right]!=-1)
10         return dp[left][right];
11     else if(left==right-1)
12     {
13         return dp[left][right]=abs(a[left]-a[right]);
14     }
15     else
16     {
17         int tp1,tp2;
18         if(a[left+1]>=a[right])
19             tp1=dfs(left+2,right)+a[left]-a[left+1];
20         else
21             tp1=dfs(left+1,right-1)+a[left]-a[right];
22         if(a[left]>=a[right-1])
23             tp2=dfs(left+1,right-1)+a[right]-a[left];
24         else
25             tp2=dfs(left,right-2)+a[right]-a[right-1];
26         return dp[left][right]=max(tp1,tp2);
27     }
28 }
29 int main()
30 {
31     int n,ca=0;
32     while(scanf("%d",&n)&&n)
33     {
34         memset(dp,-1,sizeof(dp));
35         for(int i=0;i<n;i++)
36             scanf("%d",a+i);
37         printf("In game %d, the greedy strategy might lose by as many as %d points.\n",++ca,dfs(0,n-1));
38     }
39     return 0;
40 }