MCARDS - Card Sorting

Tác giả: flashmt

Ngôn ngữ: Pascal

const fi='';
      fo='';
var n,c,re,s:longint;
    a,b,f,g:array[1..400] of longint;
    p:array[1..4,1..100] of longint;
    d:array[1..4] of longint;

procedure rf;
var i:longint;
begin
     readln(c,n);
     for i:=1 to n*c do
     begin
          readln(a[i],b[i]);
          p[a[i],b[i]]:=i;
     end;
end;

function max(x,y:longint):longint;
begin
     if x>y then max:=x else max:=y;
end;

procedure work;
var i,j,r:longint;
begin
     for i:=1 to n*c do
         f[i]:=(d[a[i]]-1)*n+b[i];
     fillchar(g,sizeof(g),0);
     for i:=2 to n*c do
         for j:=1 to i-1 do
             if f[i]>f[j] then
                g[i]:=max(g[i],g[j]+1);
     r:=0;
     for i:=1 to n*c do
         if g[i]+1>r then r:=g[i]+1;
     r:=n*c-r;
     if r<re then
        re:=r;
end;

procedure att(x:longint);
var i:longint;
begin
     if x>c then
     begin
          work;
          exit;
     end;
     for i:=1 to c do
         if d[i]=0 then
         begin
              d[i]:=x;
              att(x+1);
              d[i]:=0;
         end;
end;

procedure wf;
begin
     writeln(re);
end;

begin
     assign(input,fi); reset(input);
     assign(output,fo); rewrite(output);
     rf;
     re:=maxlongint;
     att(1);
     wf;
     close(input); close(output);
end.

Download