I and OI
Past...

基本同RQNOJ的POWER.

见我写的解题报告:http://www.cnblogs.com/exponent/archive/2011/08/08/2130723.html

code:

const oo=1000000000000000;
var   f:array[0..1010,0..1010,0..1] of int64;
      g:array[0..1010,0..1010] of longint;
      d:array[0..1010] of longint;
      n,i,j,l,p:longint;
      flag:boolean;

      procedure sort(l,r:longint);
      var   i,j,mid,temp:longint;
      begin
            i:=l; j:=r;
            mid:=d[(l+r)>>1];
            while i<=j do
            begin
                  while d[i]<mid do inc(i);
                  while d[j]>mid do dec(j);
                  if i<=j then
                  begin
                        temp:=d[i];
                        d[i]:=d[j];
                        d[j]:=temp;
                        inc(i); dec(j);
                  end;
            end;
            if i<r then sort(i,r);
            if j>l then sort(l,j);
      end;

      function min(a,b:int64):int64;
      begin
            if a>b then exit(b); exit(a)
      end;

begin
      readln(n,l);
      flag:=true;
      for i:=1 to n do
      begin
            readln(d[i]);
            if d[i]=l then flag:=false;
      end;
      if flag then
      begin inc(n); d[n]:=l; end;
      sort(1,n);

      for i:=1 to n do
         if d[i]=l then
         begin p:=i; break; end;

      for i:=1 to n do
         for j:=i to n do
         begin
               g[i,j]:=n-(j-i+1);
               if ((p<i)or(p>j))and(flag) then dec(g[i,j]);
         end;

      for i:=0 to 1010 do
         for j:=0 to 1010 do
         begin
               f[i,j,0]:=oo;
               f[i,j,1]:=oo;
         end;
      f[p,p,0]:=0; f[p,p,1]:=0;

      for j:=p to n do
         for i:=j-1 downto 1 do
         begin
               f[i,j,0]:=min(f[i+1,j,0]+g[i+1,j]*(d[i+1]-d[i]),
                             f[i+1,j,1]+g[i+1,j]*(d[j]-d[i]));
               f[i,j,1]:=min(f[i,j-1,0]+g[i,j-1]*(d[j]-d[i]),
                             f[i,j-1,1]+g[i,j-1]*(d[j]-d[j-1]));
         end;
      writeln(min(f[1,n,0],f[1,n,1]));
end.
posted on 2011-08-13 14:01  exponent  阅读(307)  评论(0编辑  收藏  举报