bzoj 3594 树状数组

题意:给定一个n个数的序列,可以最多任意选择k个区间并将区间内的数都加1,求操作后LIS的长度

我们用f[i,j]表示前i个数被操作k次后LIS的长度

f[i,j]=max(f[k,l])+1 (1<=k

三维偏序..用二维树状数组优化取max的过程

注意:用二维树状数组第一维的上界是max(a[i])+k,,第二维不能是[0,k]而是[1,k+1]

uses math;
var
        n,m,k,ans,tt    :longint;
        i,j             :longint;
        a               :array[0..10010] of longint;
        t               :array[0..6010,0..510] of longint;

function _add(x,y,v:longint):longint;
var
        tt:longint;
begin
   while x<=m+k do
   begin
      tt:=y;
      while tt<=k+1 do
      begin
         t[x,tt]:=max(t[x,tt],v);
         inc(tt,tt and (-tt));
      end;
      inc(x,x and (-x));
   end;
end;

function find(x,y:longint):longint;
var
        tt,ans:longint;
begin
   ans:=0;
   while x>0 do
   begin
      tt:=y;
      while tt>0 do
      begin
         ans:=max(ans,t[x,tt]);
         dec(tt,tt and (-tt));
      end;
      dec(x,x and (-x));
   end;
   exit(ans);
end;

begin
   read(n,k); ans:=0; m:=0;
   for i:=1 to n do read(a[i]);
   for i:=1 to n do m:=max(a[i],m);
   for i:=1 to n do
     for j:=k downto 0 do
     begin
        tt:=find(a[i]+j,j+1)+1;
        ans:=max(ans,tt);
        _add(a[i]+j,j+1,tt);
     end;
   writeln(ans);
end.
——by Eirlys

bzoj 3594 树状数组_第1张图片

你可能感兴趣的:(树状数组,bzoj)