Guest User

Untitled

a guest
May 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. var
  2. a,b,ps1,ps2:array[0..5111]of longint;
  3. f:array[1..5111,1..5111]of longint;
  4. k,m,n,i,j:longint;
  5.  
  6. function max(const a,b:longint):longint;
  7. begin
  8. if a>b then max:=a else max:=b;
  9. end;
  10.  
  11. function find(x,y:longint):longint;
  12. var
  13. j,res:longint;
  14. begin
  15. res:=0;
  16. while x>0 do
  17. begin
  18. j:=y;
  19. while j>0 do
  20. begin
  21. res:=max(res,f[x,j]);
  22. j:=j and (j-1);
  23. end;
  24. x:=x and (x-1);
  25. end;
  26. find:=res;
  27. end;
  28.  
  29. procedure modify(x,y,zn:longint);
  30. var
  31. i,j:longint;
  32. begin
  33. while x<=n do
  34. begin
  35. j:=y;
  36. while j<=m do
  37. begin
  38. f[x,j]:=max(f[x,j],zn);
  39. j:=(j or (j-1))+1;
  40. end;
  41. x:=(x or (x-1))+1;
  42. end;
  43. end;
  44.  
  45. begin
  46. assign(input,'interview.in'); reset(input);
  47. assign(output,'interview.out'); rewrite(output);
  48. readln(n,m);
  49. for i:=1 to n do read(a[i]);
  50. for i:=1 to n do ps1[i]:=i;
  51. for i:=1 to m do read(b[i]);
  52. for i:=1 to m do ps2[i]:=i;
  53.  
  54. 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
  55. begin
  56. a[0]:=a[i]; a[i]:=a[j]; a[j]:=a[0];
  57. ps1[0]:=ps1[i]; ps1[i]:=ps1[j]; ps1[j]:=ps1[0];
  58. end;
  59.  
  60. 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
  61. begin
  62. b[0]:=b[i]; b[i]:=b[j]; b[j]:=b[0];
  63. ps2[0]:=ps2[i]; ps2[i]:=ps2[j]; ps2[j]:=ps2[0];
  64. end;
  65.  
  66. for i:=1 to n do for j:=1 to m do if a[i]=b[j] then
  67. begin
  68. k:=find(ps1[i]-1,ps2[j]-1)+1;
  69. modify(ps1[i],ps2[j],k);
  70. end;
  71. writeln(find(n,m));
  72. close(output);
  73. end.
Add Comment
Please, Sign In to add comment