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, h, 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; h:=1;
  49. while h<10 do begin
  50. count:=0;
  51. while i<lung do
  52. begin
  53. if (rimossi[i]<rimossi[i-1]) and (rimossi[i]<rimossi[i+1])
  54. then
  55. begin
  56. if (i+count)<lung then valli[i+count]:=-1;
  57. delete(rimossi,i,1);
  58. lung:=lung-1; count:=count+1;
  59. setLength(rimossi,lung); i:=i+1;
  60. end
  61. else i:=i+1;
  62. end;
  63. h:=h+1;
  64. end;
  65. ANS := 0;
  66. (*leftLIS[i] stores the length of longest increasing subsequence ending at index i*)
  67. (*rightLIS[i] stores the length of longest decreasing subsequence starting at index i*)
  68. len:=1; SetLength(LIS,len); LIS[0]:=P[0];
  69. for i:=0 to N-1 do begin leftLIS[i]:=1; rightLIS[i]:=1; end;
  70. (*Calculate LIS from left to right for each position*)
  71. for i :=1 to N-1 do
  72. begin
  73. ricercaUpper(Lis, P[i]);
  74. // if element in not present in lis insert at the end
  75.  
  76. if ((id=-1) and (valli[i]=0)) 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) and (valli[i]=0)) 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. (* 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) and (valli[i]=0)) 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]=0)) 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 5304KB
stdin
7
1 4 0 2 3 6 5
stdout
3