I and OI
Past...

题意:就是求最长上升子序列.

分析:N^2DP会超时,用NlogN的算法.

code:

var   a:array[0..40001] of longint;
      datanum,d,n,i,p,top,num:longint;

      procedure find(key,l,r:longint);
      var   mid:longint;
      begin
            if l=r then
            begin p:=l; exit; end;
            mid:=(l+r)>>1;
            if a[mid]>key then find(key,l,mid)
            else find(key,mid+1,r);
      end;

begin
      readln(datanum);
      for d:=1 to datanum do
      begin
            fillchar(a,sizeof(a),0);
            readln(n);
            readln(num);
            a[1]:=num;
            top:=1;
            for i:=2 to n do
            begin
                  readln(num);
                  if num>a[top] then
                     begin inc(top); a[top]:=num; end
                  else
                     begin
                           find(num,1,top);
                           a[p]:=num;
                     end;
            end;
            writeln(top);
      end;
end.

posted on 2011-08-10 15:43  exponent  阅读(143)  评论(0编辑  收藏  举报