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, ricordaANS : 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; ricordaANS:=100004; ANS := 0;
  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. for i:=1 to lung do P[i-1]:=StrToInt(rimossi[i]);
  60. if uscita=true then continue
  61. else
  62. begin
  63.  
  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 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. begin
  82. len:=len+1;
  83. SetLength(LIS,len);
  84. LIS[len -1] := P[i];
  85. leftLIS[i]:=len;
  86. end;
  87. end;
  88.  
  89. (* Calculate LIS from right to left (decreasing subsequence) for each position*)
  90.  
  91. len:=1; SetLength(LIS,len); LIS[0]:=P[N-1];
  92. for i :=lung-2 downto 0 do
  93. begin
  94. ricercaUpper(Lis, P[i]);
  95.  
  96. // if element is to be inserted in lis
  97.  
  98. if id <>-1 then
  99. begin
  100. LIS[id] := P[i];
  101. rightLIS[i]:=id+1;
  102. end
  103. // if element in not present in lis insert at the end
  104. else
  105. begin
  106. len:=len+1;
  107. SetLength(LIS,len);
  108. LIS[len -1] := P[i];
  109. rightLIS[i]:=len;
  110. end;
  111. end;
  112.  
  113. maxMountainLength := 0;
  114. (* Find the maximum length of mountain subsequence*)
  115. // for every index check for longest mountain array,
  116. for i := 0 to lung-1 do
  117. begin
  118. if (leftLIS[i] >=1) AND (rightLIS[i] >= 1) then
  119. begin
  120. x := leftLIS[i] + rightLIS[i] - 1;
  121. maxMountainLength := max(maxMountainLength, x);
  122. writeln (maxMountainLength, ' ciao ');
  123.  
  124. end;
  125. end;
  126. // returning removals
  127.  
  128. ANS:= N - maxMountainLength;
  129. if ANS<ricordaANS then ricordaANS:=ANS;
  130. end;
  131. WriteLn(ricordaANS); end;
  132. end.
Success #stdin #stdout 0s 5320KB
stdin
3
0 1 2
stdout
Standard output is empty