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, ricordamaxmount : 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.  
  45. uscita:=false; ricordamaxmount:=0;
  46. while uscita=false do
  47. begin
  48. j:=2; uscita:=true;
  49. while j<lung do
  50. begin
  51. if (rimossi[j]<rimossi[j-1]) and (rimossi[j]<rimossi[j+1])
  52. then
  53. begin
  54. delete(rimossi,j,1);
  55. lung:=lung-1;
  56. setLength(rimossi,lung);
  57. uscita:=false;
  58. end;
  59. for i:=1 to lung do P[i-1]:=StrToInt(rimossi[i]);
  60. ANS := 0;
  61. (*leftLIS[i] stores the length of longest increasing subsequence ending at index i*)
  62. (*rightLIS[i] stores the length of longest decreasing subsequence starting at index i*)
  63. len:=1; SetLength(LIS,len); LIS[0]:=P[0];
  64. for i:=0 to lung-1 do begin leftLIS[i]:=1; rightLIS[i]:=1; end;
  65. (*Calculate LIS from left to right for each position*)
  66. for i :=1 to lung-1 do
  67. begin
  68. ricercaUpper(Lis, P[i]);
  69. // if element is to be inserted in lis
  70.  
  71. if (id <>-1) and (id<>0) then
  72. begin
  73. LIS[id] := P[i];
  74. leftLIS[i]:=id+1;
  75. end
  76. // if element in not present in lis insert at the end
  77. else
  78. if id=-1 then
  79. begin
  80. len:=len+1;
  81. SetLength(LIS,len);
  82. LIS[len-1] := P[i];
  83. leftLIS[i]:=len;
  84. end;
  85. end;
  86.  
  87. (* Calculate LIS from right to left (decreasing subsequence) for each position*)
  88.  
  89. len:=1; SetLength(LIS,len); LIS[0]:=P[N-1];
  90. for i :=lung-2 downto 0 do
  91. begin
  92. ricercaUpper(Lis, P[i]);
  93.  
  94. // if element is to be inserted in lis
  95.  
  96. if (id <>-1) and (id<>0) then
  97. begin
  98. LIS[id] := P[i];
  99. rightLIS[i]:=id+1;
  100. end
  101. // if element in not present in lis insert at the end
  102. else
  103. if id=-1 then
  104. begin
  105. len:=len+1;
  106. SetLength(LIS,len);
  107. LIS[len-1] := P[i];
  108. rightLIS[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 := 0 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. if maxMountainLength>ricordamaxmount then ricordamaxmount:=maxMountainLength;
  125.  
  126.  
  127. j:=j+1;
  128. end;
  129. end;
  130.  
  131.  
  132. ANS:= N - ricordamaxmount;
  133. WriteLn(ANS);
  134. end.
Success #stdin #stdout 0s 5304KB
stdin
7 
6 2 3 0 5 1 4
stdout
4