Cod sursa(job #58428)

Utilizator gurneySachelarie Bogdan gurney Data 5 mai 2007 21:02:42
Problema Cutii Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.37 kb
program cutii;
  const
    fin='cutii.in';
    fout='cutii.out';
    nmax=3500;
  type
    box=record
      x,y,z:longint;
      end;
  var
    a:array[1..nmax] of box;
    nr:array[0..nmax] of longint;
    st,dr,mid,i,j,k,n,it,t,x,y,z,max,best:longint;
    mex:array[0..nmax] of longint;

procedure scufunda(i,n:integer);
  var
    p:integer;
    aux:box;
  begin
    p:=i;
    if i shl 1<=n then
      if (a[i shl 1].x<a[i].x)or((a[i shl 1].x=a[i].x)and(a[i shl 1].y<a[i shl 1].y)) then
        p:=i shl 1
      else if ((a[i shl 1].x=a[i].x)and(a[i shl 1].y=a[i].y))and(a[i shl 1].z<a[i].z) then
        p:=i shl 1;
    if i shl 1 or 1<=n then
      if (a[i shl 1 or 1].x<a[p].x)or((a[i shl 1 or 1].x=a[p].x)and(a[i shl 1 or 1].y<a[p].y)) then
        p:=i shl 1 or 1
      else if ((a[i shl 1 or 1].x=a[p].x)and(a[i shl 1 or 1].y=a[p].y))and(a[i shl 1 or 1].z<a[p].z) then
        p:=i shl 1 or 1;
    if p<>i then
      begin
        aux:=a[i];a[i]:=a[p];a[p]:=aux;
        scufunda(p,n);
      end;
  end;

function ok(i,j:integer):boolean;
  begin
    ok:=((a[i].x<a[j].x)and(a[i].y<a[j].y))and(a[i].z<a[j].z);
  end;

procedure hsort(n:integer);
  var
    i:integer;
    aux:box;
  begin
    for i:=n shr 1 downto 1 do
      scufunda(i,n);
    for i:=n downto 2 do
      begin
        aux:=a[1];a[1]:=a[i];a[i]:=aux;
        scufunda(1,i-1);
      end;
  end;

begin
  assign(input,fin);
  assign(output,fout);
    reset(input);
    rewrite(output);
    readln(n,t);
    for it:=1 to t do
      begin
        best:=0;
        a[n+1].x:=nmax+1;a[n+1].y:=nmax+1;a[n+1].z:=nmax+1;
        for i:=1 to n do
          readln(a[i].x,a[i].y,a[i].z);
        hsort(n);
        fillchar(nr,sizeof(nr),0);
        for i:=1 to n do
          begin
            max:=0;
            for j:=(i-1) downto 1 do
              begin
                if ((a[i].x<a[j].x)and(a[i].y<a[j].y))and(a[i].z<a[j].z) then
                  if nr[j]>max then
                    max:=nr[j];
                if max+1>mex[j-1] then
                  break;
              end;
            nr[i]:=max+1;
            if nr[i]>best then
              best:=nr[i];
            if nr[i]>mex[i-1] then
              mex[i]:=nr[i]
            else
              mex[i]:=mex[i-1];
          end;
        writeln(best);
      end;
  close(input);
  close(output);
end.