fork download
  1. Program scmax;
  2. { constraints }
  3. const
  4. MAXN = 100000;
  5.  
  6. { input data }
  7. var
  8. N, i, temp : longint;
  9. A : array[0..MAXN-1] of longint;
  10. T : array[1..MAXN] of longint;
  11. ft : array[1..MAXN] of longint;
  12. best : array[1..MAXN] of longint;
  13.  
  14. procedure update(i: longint; v: longint);
  15. begin
  16. i := N-i+1;
  17. while (i <= N) do
  18. begin
  19. if v > ft[i] then
  20. ft[i] := v;
  21. i := i + (i and (-i));
  22. end;
  23. end;
  24.  
  25. function get(i:longint) : longint;
  26. var res: longint;
  27. begin
  28. res := 0;
  29. i := N-i+1;
  30. while (i > 0) do
  31. begin
  32. if res < ft[i] then
  33. res := ft[i];
  34. i := i - (i and (-i));
  35. end;
  36. get := res;
  37. end;
  38.  
  39.  
  40. begin
  41. {
  42.   uncomment the following lines if you want to read/write from files
  43.   assign(input, 'input.txt'); reset(input);
  44.   assign(output, 'output.txt'); rewrite(output);
  45. }
  46.  
  47. readln(N);
  48. for i:=0 to N-1 do
  49. read(A[i]);
  50. readln();
  51. for i:=1 to N do
  52. read(T[i]);
  53. readln();
  54.  
  55. {
  56.   WARNING! T is indexed from 1!
  57.   In particular T[i] is the preferred number of i.
  58.   }
  59.  
  60. for i:=1 to N do
  61. begin
  62. best[i] := 0;
  63. ft[i] := 0;
  64. end;
  65.  
  66. i := N-1;
  67. while i >= 0 do
  68. begin
  69. if best[T[A[i]]]+1 > best[A[i]] then
  70. best[A[i]] := best[T[A[i]]]+1;
  71. temp := get(A[i]+1)+1;
  72. if temp > best[A[i]] then
  73. best[A[i]] := temp;
  74. update(A[i], best[A[i]]);
  75. i := i-1;
  76. end;
  77.  
  78.  
  79.  
  80. { insert your code here }
  81.  
  82. writeln(get(1)); { print result }
  83. end.
  84.  
Success #stdin #stdout 0s 5284KB
stdin
6
1 2 3 1 2 3
6 5 1 3 2 1
stdout
6