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