Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var
- a,b,ps1,ps2:array[0..5111]of longint;
- f:array[1..5111,1..5111]of longint;
- k,m,n,i,j:longint;
- function max(const a,b:longint):longint;
- begin
- if a>b then max:=a else max:=b;
- end;
- function find(x,y:longint):longint;
- var
- j,res:longint;
- begin
- res:=0;
- while x>0 do
- begin
- j:=y;
- while j>0 do
- begin
- res:=max(res,f[x,j]);
- j:=j and (j-1);
- end;
- x:=x and (x-1);
- end;
- find:=res;
- end;
- procedure modify(x,y,zn:longint);
- var
- i,j:longint;
- begin
- while x<=n do
- begin
- j:=y;
- while j<=m do
- begin
- f[x,j]:=max(f[x,j],zn);
- j:=(j or (j-1))+1;
- end;
- x:=(x or (x-1))+1;
- end;
- end;
- begin
- assign(input,'interview.in'); reset(input);
- assign(output,'interview.out'); rewrite(output);
- readln(n,m);
- for i:=1 to n do read(a[i]);
- for i:=1 to n do ps1[i]:=i;
- for i:=1 to m do read(b[i]);
- for i:=1 to m do ps2[i]:=i;
- for i:=1 to n do for j:=i+1 to n do if (a[j]<a[i])or(a[j]=a[i])and(ps1[i]<ps1[j]) then
- begin
- a[0]:=a[i]; a[i]:=a[j]; a[j]:=a[0];
- ps1[0]:=ps1[i]; ps1[i]:=ps1[j]; ps1[j]:=ps1[0];
- end;
- for i:=1 to m do for j:=i+1 to m do if (b[j]<b[i])or(b[j]=b[i])and(ps2[i]<ps2[j]) then
- begin
- b[0]:=b[i]; b[i]:=b[j]; b[j]:=b[0];
- ps2[0]:=ps2[i]; ps2[i]:=ps2[j]; ps2[j]:=ps2[0];
- end;
- for i:=1 to n do for j:=1 to m do if a[i]=b[j] then
- begin
- k:=find(ps1[i]-1,ps2[j]-1)+1;
- modify(ps1[i],ps2[j],k);
- end;
- writeln(find(n,m));
- close(output);
- end.
Add Comment
Please, Sign In to add comment