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