MCARDS - Card Sorting
Tác giả: ll931110
Ngôn ngữ: Pascal
{$MODE DELPHI}
Program MCARDS;
Const
input = '';
output = '';
maxv = 1000;
maxn = 100;
maxc = 4;
Type
rec = record
col,val: integer;
end;
Var
a: array[1..maxn * maxc] of rec;
free: array[1..maxc] of boolean;
h: array[1..maxc] of integer;
p,len: array[1..maxn * maxc] of integer;
n,c,maxlen: integer;
Procedure init;
Var
f: text;
i: integer;
Begin
Assign(f, input);
Reset(f);
Readln(f, c, n);
For i:= 1 to n * c do readln(f, a[i].col, a[i].val);
Close(f);
Fillchar(free, sizeof(free), true);
maxlen:= 0;
End;
Procedure LIncSeq;
Var
i,j,curr: integer;
Begin
For i:= 1 to n * c do p[i]:= h[a[i].col] * maxv + a[i].val;
Fillchar(len, sizeof(len), 0);
len[1]:= 1;
curr:= 1;
For i:= 2 to n * c do
Begin
len[i]:= 1;
For j:= 1 to i - 1 do
if (p[j] < p[i]) and (len[j] + 1 > len[i]) then len[i]:= len[j] + 1;
If curr < len[i] then curr:= len[i];
End;
If maxlen < curr then maxlen:= curr;
End;
Procedure attempt(i: integer);
Var
j: integer;
Begin
For j:= 1 to c do
If free[j] then
Begin
free[j]:= false;
h[i]:= j;
If i = c then LIncSeq else attempt(i + 1);
free[j]:= true;
End;
End;
Procedure printresult;
Var
f: text;
Begin
Assign(f, output);
Rewrite(f);
Writeln(f, n * c - maxlen);
Close(f);
End;
Begin
init;
attempt(1);
printresult;
End.