The main topic: give the number of N (≤500), two people take a number of turns, each can be from the left or right of the sequence to take a number, until all the number is taken out, two people are the best strategy to take the number, the last two people to score.
Obviously this type of game problem, the first eye is a great minimum of search + memory, but I am not very very small search tat. Then the second eye found that it could be written with a shape, and obviously better than a very small search. Preprocessing the prefix and then F[i,j] indicates the maximum score to be obtained from the number of I to the number of J, then F[i,j]=sum[j]-sum[i-1]-min (F[i+1,j],f[i,j-1]); "The number of I to the number of J and minus min (Section i+ 1 numbers to the number of J number of the first can get the maximum score, the number of I to the number of j-1 first can get the maximum score) "
To note that because F[I,J] need to use f[i+1,j] and f[i,j-1], so we need to enumerate the length of the interval I to J, the interval length of a small calculation, in order to calculate the length of the interval is large, the beginning of this hole, I am still too weak = ...
The code is as follows:
varN,i,j,x:longint; F:Array[0.. -,0.. -] ofLongint; Sum:Array[0.. -] ofLongint;functionmin (a,b:longint): Longint;begin ifA<b Thenexit (a); Exit (b);End;beginREADLN (n); fori:=1 toN Do beginread (x); Sum[i]:=sum[i-1]+x;//prefix and f[i,i]:=x; End; forj:=1 toN-1 Do//enumeration Interval Length fori:=1 toN-j Do//Enumeration Start F[i,i+j]:=sum[i+j]-sum[i-1]-min (f[i+1, i+j],f[i,i+j-1]); Writeln (f[1, N],' ', sum[n]-f[1, n]);//sum[n]-f[1, N]End.
View Code
Of course, I will not be very small search is because I am konjac konjac ah ... This problem hr God Ben with a very small search, it is very god%%%.
DFS (L,R) indicates that the number of L has been taken on the left, the number of R has been taken on the right, the remaining number of points, the maximum number of times than the opponent, there is Dfs (L,R): =max (A[l+1]-dfs (l+1,r), A[n-r]-dfs (l,r+1)); Because both sides use the optimal strategy, so many points more than the opponent =max (take the left number-the opponent next most than you score, take the right number-the opponent next most than you how many points); then the initiator score is (the total number of points + the maximum number of votes than the hand) div 2, then the score is (score-the maximum number of times than the hand) Div 2.
The code is as follows:
varN,i,j,res,sum:longint; A:Array[0.. +] ofLongint; F:Array[0.. -,0.. -] ofLongint;functionMax (a.b:longint): Longint;begin ifA>b Thenexit (a); Exit (b);End;functionDFS (l,r:longint): Longint;begin ifF[l,r]<>maxlongint Thenexit (F[l,r]); ifL+r=n Then beginF[l,r]:=0; Exit (0); End; F[L,R]:=max (a[l+1]-dfs (L +1, R), A[n-r]-dfs (l,r+1)); Exit (F[l,r]);End;beginREADLN (n); Sum:=0; fori:=1 toN Do beginread (a[i]); Inc (Sum,a[i]); End; fori:=0 toN Do forj:=0 toN-i DoF[i,j]:=Maxlongint; Res:=dfs (0,0); Writeln (Sum+f[0,0])Div 2,' ', (sum-f[0,0])Div 2);End.
View Code
[CodeVs3196] Gold treasure (state compression dp/Max min search)