fork download
  1. program mountain;
  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;
  56. delete(rimossi,i,1);
  57. lung:=lung-1; count:=count+1;
  58. setLength(rimossi,lung); i:=i+1;
  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. if valli[i]=0 then
  72. begin
  73. ricercaUpper(Lis, P[i]);
  74. // if element in not present in lis insert at the end
  75.  
  76. if id=-1 then
  77. begin
  78. len:=len+1;
  79. SetLength(LIS,len);
  80. LIS[len-1] := P[i];
  81. leftLIS[i]:=len;
  82. end
  83. else
  84. begin
  85. // if element is to be inserted in lis
  86. if (id<>0) and (valli[id]=-1) then
  87. begin
  88. LIS[id] := P[i];
  89. leftLIS[i]:=id+1;
  90. end
  91. else
  92. leftLIS[i]:=0;
  93.  
  94. end;
  95. end
  96.  
  97. else leftLIS[i]:=0;
  98. end;
  99.  
  100. (* Calculate LIS from right to left (decreasing subsequence) for each position*)
  101.  
  102. len:=1; SetLength(LIS,len); LIS[0]:=P[N-1];
  103.  
  104. for i :=N-2 downto 0 do
  105. begin
  106. ricercaUpper(Lis, P[i]);
  107. if id=-1 then
  108. begin
  109. len:=len+1;
  110. SetLength(LIS,len);
  111. LIS[len-1] := P[i];
  112. rightLIS[i]:=len;
  113. end
  114. else
  115. begin
  116. // if element is to be inserted in lis
  117. if (id<>0) and (valli[N-1-id]=-1) then
  118. begin
  119. LIS[id] := P[i];
  120. rightLIS[i]:=id+1;
  121. end
  122. else
  123. rightLIS[i]:=0;
  124.  
  125.  
  126. end;
  127. end;
  128.  
  129. maxMountainLength := 0;
  130. (* Find the maximum length of mountain subsequence*)
  131. // for every index check for longest mountain array,
  132. for i := 0 to N-1 do
  133. begin
  134. if (leftLIS[i] >=1) AND (rightLIS[i] >= 1) then
  135. begin
  136. x := leftLIS[i] + rightLIS[i] - 1;
  137. maxMountainLength := max(maxMountainLength, x);
  138. end;
  139. end;
  140. // returning removals
  141.  
  142. ANS:= N - maxMountainLength;
  143. WriteLn(ANS);
  144. end.
Success #stdin #stdout 0s 5324KB
stdin
6
2 3 0 5 1 4
stdout
3