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