fork download
  1. program mountain;(*21 punti 23 24 25 26 corretti*)
  2. Uses sysutils, Math;
  3.  
  4. const
  5. MAXN = 100005;
  6.  
  7. Type elenco= Array of LongInt;
  8.  
  9. var
  10. ANS, N, i, id, x, maxMountainLength, lung, len, count : LongInt;
  11. P, leftLIS, rightLIS, valli: Array[0..MAXN-1] of LongInt;
  12. LIS : elenco;
  13. rimossi : Ansistring;
  14.  
  15.  
  16.  
  17. Procedure ricercaUpper (var w:elenco; target:Longint); (*ritorna indice del valore maggiore/uguale a target oppure -1 se non esiste*)
  18. var m,start,eend: Longint;
  19.  
  20. begin
  21. start:=0; eend:=len-1 ; m:=-1;
  22. while start<=eend do
  23. begin
  24. m:=(start + eend) div 2;
  25. if w[m]<target then start:=m+1
  26. else if w[m]>=target then begin id:=m; eend:=m-1 end;
  27. end;
  28. if start=len then id:=-1;
  29.  
  30. end;
  31.  
  32.  
  33.  
  34.  
  35. begin
  36. (*assign(input, 'input.txt'); reset(input);
  37.   assign(output, 'output.txt'); rewrite(output); *)
  38.  
  39. ReadLn(N);
  40. rimossi:=''; lung:=N;
  41. for i:=0 to N-1 do begin
  42. Read(P[i]);
  43. rimossi:=rimossi+IntTostr(P[i]);
  44. valli[i]:=0;
  45. end;
  46. ReadLn();
  47. count:=0;
  48. i:=2;
  49.  
  50. while i<=lung do
  51. begin
  52. if (rimossi[i]<rimossi[i-1]) and (rimossi[i]<rimossi[i+1])
  53. then
  54. begin
  55. if (i+count)<lung then valli[i+count-1]:=-1;
  56. delete(rimossi,i,1);
  57. lung:=lung-1; count:=count+1;
  58. setLength(rimossi,lung);
  59. end
  60. else i:=i+1;
  61. end;
  62.  
  63. ANS := 0;
  64. (*leftLIS[i] stores the length of longest increasing subsequence ending at index i*)
  65. (*rightLIS[i] stores the length of longest decreasing subsequence starting at index i*)
  66. len:=1; SetLength(LIS,len); LIS[0]:=P[0];
  67. for i:=0 to N-1 do begin leftLIS[i]:=1; rightLIS[i]:=1; end;
  68. (*Calculate LIS from left to right for each position*)
  69. for i :=1 to N-1 do
  70. begin
  71.  
  72. ricercaUpper(Lis, P[i]);
  73. // if element in not present in lis insert at the end
  74.  
  75. if id=-1 then
  76. begin
  77. len:=len+1;
  78. SetLength(LIS,len);
  79. LIS[len-1] := P[i];
  80. leftLIS[i]:=len;
  81. end
  82. else
  83. begin
  84. // if element is to be inserted in lis
  85. if ((id<>0) and (valli[id]=-1) and (valli[i]<>-1)) then
  86. begin
  87. LIS[id] := P[i];
  88. leftLIS[i]:=id+1;
  89. end
  90. else
  91. leftLIS[i]:=0;
  92.  
  93. end;
  94.  
  95. end;
  96.  
  97. (* Calculate LIS from right to left (decreasing subsequence) for each position*)
  98.  
  99. len:=1; SetLength(LIS,len); LIS[0]:=P[N-1];
  100.  
  101. for i :=N-2 downto 0 do
  102. begin
  103. ricercaUpper(Lis, P[i]);
  104. if id=-1 then
  105. begin
  106. len:=len+1;
  107. SetLength(LIS,len);
  108. LIS[len-1] := P[i];
  109. rightLIS[i]:=len;
  110. end
  111. else
  112. begin
  113. // if element is to be inserted in lis
  114. if (id<>0) and (valli[N-1-id]=-1) and (valli[i]<>-1) then
  115. begin
  116. LIS[id] := P[i];
  117. rightLIS[i]:=id+1;
  118. end
  119. else
  120. rightLIS[i]:=0;
  121.  
  122.  
  123. end;
  124. end;
  125.  
  126. maxMountainLength := 0;
  127. (* Find the maximum length of mountain subsequence*)
  128. // for every index check for longest mountain array,
  129. for i := 0 to N-1 do
  130. begin
  131. if (leftLIS[i] >=1) AND (rightLIS[i] >= 1) then
  132. begin
  133. x := leftLIS[i] + rightLIS[i] - 1;
  134. maxMountainLength := max(maxMountainLength, x);
  135. end;
  136. end;
  137. // returning removals
  138.  
  139. ANS:= N - maxMountainLength;
  140. WriteLn(ANS);
  141. end.
Success #stdin #stdout 0s 5288KB
stdin
7
0 6 1 2 3 5 4
stdout
3