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