Topic Connection:Sicily 1176
Problem Solving Ideas:
The title looks like a game of the topic, and like an interval DP (matrix fetch), and it is the difference between the number of the matrix is that he is two people in the number, so each time to an interval, we should be divided into two cases: the first person to take the left and the number of the right, and in the separate consideration of both cases, We are going to get the interval of the previous fetch according to the greedy law. The state equation is a bit complex, directly on the code:
//problem#: 1176//submission#: 3601655//The source code is licensed under Creative Commons attribution-noncommercial-sharealike 3.0 Unported License//uri:http://creativecommons.org/licenses/by-nc-sa/3.0///All Copyright reserved by informatic Lab of Sun Yat-sen University#include <bits/stdc++.h>#define INF 0x3f3f3f3fusing namespace STD;intn,a[1005],dp[1005][1005];intMain () {intt=1; while(~scanf("%d", &n) &&n) { for(intI=1; i<=n;i++)scanf("%d", &a[i]);memset(Dp,-inf,sizeof(DP)); for(intlen=2; len<=n;len+=2) { for(intI=1; i<=n-len+1; i++) {intj=i+len-1;if(j-i==1) Dp[i][j]=max (a[i],a[i+1])-min (a[i],a[i+1]);Else{inttmpif(a[i+1]>=A[J])//Take the number on the lefttmp=dp[i+2][j]+a[i]-a[i+1];Elsetmp=dp[i+1][j-1]+A[I]-A[J]; dp[i][j]=tmp;if(a[i]>=a[j-1])//Take the number on the righttmp=dp[i+1][j-1]+a[j]-a[i];Elsetmp=dp[i][j-2]+a[j]-a[j-1]; Dp[i][j]=max (DP[I][J],TMP); } } }printf("In game%d, the greedy strategy might lose by as many as%d points.\n", t++,dp[1][n]); }return 0;}
Summary:
In fact, a dynamic programming of the idea to do the game of the topic, you can learn from the matrix to take the number.
Sicily 1176 (Dynamic planning)