Hướng dẫn giải của Card Sorting
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của flashmt
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.
Code mẫu của happyboy99x
#include<cstdio> #include<algorithm> using namespace std; int p[] = {0, 1, 2, 3}, c, n; struct card { int col, val; bool operator < (const card &o) const { return col != o.col ? p[col] < p[o.col] : val < o.val; } } a[444], last[444]; void enter() { scanf("%d%d", &c, &n); for(int i = 0; i < c * n; ++i) { scanf("%d%d", &a[i].col, &a[i].val); --a[i].col; } } void solve() { int res = n * c; do { int maxlen = 0; for(int i = 0; i < c * n; ++i) { int dp = lower_bound(last, last+maxlen, a[i]) - last; if(dp >= maxlen) last[maxlen++] = a[i]; else last[dp] = min(last[dp], a[i]); } res = min(res, n * c - maxlen); } while (next_permutation(p, p+c)); printf("%d\n", res); } int main() { enter(); solve(); return 0; }
Code mẫu của ladpro98
program mcards; uses math; const maxN=444; base=1234; fi=''; type e=record c,v:longint; end; var list:array[1..maxN] of e; a,f:array[1..maxN] of longint; check:array[1..4] of boolean; p:array[1..4] of longint; n,c,d,res:longint; procedure input; var inp:text; i:longint; begin assign(inp,fi); reset(inp); readln(inp,c,n); d:=c*n; for i:=1 to d do readln(inp,list[i].c,list[i].v); close(inp); res:=maxN; end; function find(r,key:longint):longint; var m,k,l:longint; begin l:=1; k:=0; while l<=r do begin m:=(l+r) shr 1; if a[f[m]]<key then begin k:=m; l:=m+1 end else r:=m-1; end; exit(k); end; procedure solve; var i,j,t:longint; begin for i:=1 to d do begin for j:=1 to c do if list[i].c=p[j] then a[i]:=j*base+list[i].v; end; //lis t:=1;f[1]:=1; for i:=2 to d do begin if a[i]<a[f[1]] then f[1]:=i else if a[i]>a[f[t]] then begin inc(t); f[t]:=i; end else f[find(t,a[i])+1]:=i; end; res:=min(res,d-t); end; procedure back(i:longint); var j:longint; begin if i>c then solve else begin for j:=1 to c do if not check[j] then begin check[j]:=true; p[i]:=j; back(i+1); check[j]:=false; end; end; end; begin input; back(1); write(res); end.
Code mẫu của RR
//Written by Nguyen Thanh Trung {$R+,Q+} {$Mode objFPC} uses math; const FINP = ''; FOUT = ''; MAXN = 444; type cards = record u,color,val:longint; end; var f1,f2 : text; best,n,k : longint; a : array[1..MAXN] of cards; d : array[1..MAXN] of longint; dd,pos : array[1..4] of longint; procedure openF; begin assign(f1,FINP); reset(f1); assign(f2,FOUT); rewrite(f2); end; procedure closeF; begin close(f1); close(f2); end; procedure inp; var i:longint; begin read(f1,k,n); for i:=1 to n*k do with a[i] do read(f1,color,u); end; procedure solve; var i,j:longint; begin for i:=n*k downto 1 do with a[i] do val:=pos[color]*n+u; for i:=1 to n*k do begin d[i]:=1; for j:=i-1 downto 1 do if a[j].val<a[i].val then d[i]:=max(d[i],d[j]+1); best:=max(best,d[i]); end; end; procedure dequy(i:longint); var j:longint; begin for j:=1 to k do if dd[j]=0 then begin dd[j]:=1; pos[j]:=i-1; if i<k then dequy(i+1) else solve; dd[j]:=0; end; end; begin openF; inp; dequy(1); writeln(f2,n*k-best); closeF; end.
Code mẫu của hieult
#include <cstdio> //#include <conio.h> struct st_card { int color,value; }; st_card A[412]; int main() { //freopen("MCARDS.in","r",stdin); int colors,numbers,cards,a[6],b[6],giaithua[6],the,chia,f[414],x[414],gan; scanf("%d %d",&colors,&numbers); cards = colors*numbers; for(int i = 1;i<= cards;i++) scanf("%d %d",&A[i].color,&A[i].value); giaithua[0] = 1; for(int i = 1;i<=colors;i++) giaithua[i] = giaithua[i-1]*i; int KQ = cards; for(int ii = 0;ii < giaithua[colors];ii++) { //printf("1"); the = ii;for(int i = 1; i <= colors ;i++) b[i] = i; for(int i = 1;i<=colors;i++) { chia = the/giaithua[colors-i]; a[i] = b[chia+1]; for(int j = chia+1;j<=colors-i;j++) b[j] = b[j+1]; the = the%giaithua[colors-i]; } for(int i = 1;i<=cards;i++) x[i] = a[A[i].color]*numbers+A[i].value; for(int i = 1;i<=cards;i++) { gan = 1; for(int j = 1;j<i;j++) if(x[j]<x[i] && gan<f[j]+1) gan = f[j]+1; f[i] = gan; if(cards - gan <KQ) KQ = cards-gan; } } printf("%d",KQ); //getch(); }
Code mẫu của ll931110
{$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.
Code mẫu của khuc_tuan
//{$APPTYPE CONSOLE} {$MODE DELPHI} uses SysUtils, Math; var res, i, n, m : integer; f, a, value, color : array[0..400] of integer; used : array[1..4] of boolean; h : array[1..4] of integer; procedure duyet(pos : integer); var i, j : integer; begin if pos > m then begin for i:=1 to n * m do begin for j:=1 to m do if color[i]=h[j] then break; a[i] := j * 1000 + value[i]; end; for i:= 1 to n * m do begin f[i] := 1; for j:=1 to i-1 do if (a[j] < a[i]) and (f[j] + 1 > f[i]) then f[i] := f[j] + 1; if res < f[i] then res := f[i]; end; exit; end; for i:=1 to m do if not used[i] then begin h[pos] := i; used[i] := true; duyet( pos + 1); used[i] := false; end; end; begin read(m, n); for i:=1 to n * m do begin read(color[i], value[i]); end; duyet(1); writeln(n*m - res); end.
Bình luận