I and OI
Past...

首先O(N^2)的DP挺好想的.

f[i,j]=sum[i,j]-min(f[i+1,j],f[i,j-1]).

但题目把n出到5000,内存卡到64M,二维的状态存不下..

其实,j这一维可以省掉.我们换个状态表示

f[i,i+len]=sum[i,i+len]-min(f[i+1,i+len],f[i,i+len-1])

然后循环这样写:

for len=1 to n

   for i=1 to n-len.

容易看出第二维可以省掉了.

code:

/************************************************************** 
    Problem: 2101
    User: exponent 
    Language: Pascal 
    Result: Accepted 
    Time:332 ms 
    Memory:264 kb 
****************************************************************/ 
  
var   s,f:array[0..5001] of longint; 
      n,i,l,num:longint; 
  
      function min(a,b:longint):longint; 
      begin
            if a>b then exit(b); exit(a); 
      end; 
  
begin
      readln(n); 
      for i:=1 to n do
      begin
            read(num); 
            s[i]:=s[i-1]+num; 
            f[i]:=num; 
      end; 
  
      for l:=1 to n-1 do
         for i:=1 to n-l do
         f[i]:=s[i+l]-s[i-1]-min(f[i],f[i+1]); 
  
      writeln(f[1]); 
end.
posted on 2011-08-14 13:02  exponent  阅读(239)  评论(0编辑  收藏  举报