fork download
  1. program monuments;
  2. Uses Math;
  3. const
  4. MAXN = 2000000;
  5. type elenco = array[0..MAXN-1] of longint;
  6. var
  7. N, K, L, i : longint;
  8. A : elenco;
  9.  
  10. function demolish(N, K, L: longint; var A: elenco): longint;
  11. var inizio, fine, h, ans: Longint;
  12. M : array [0..2*MAXN-1] of longint;
  13. begin
  14. if N=0 then demolish:=0
  15. else
  16. begin
  17. for h := 0 to N -1 do
  18. begin
  19. M[h] := A[h];
  20. M[h+N] := A[h] + K;
  21. end;
  22. ans := N;
  23. fine := 0;
  24. for inizio:=0 to N-1 do
  25. begin
  26. while M[fine]-M[inizio] <= L do fine:=fine+1;
  27. ans := min(ans, fine-inizio-1);
  28. end;
  29. demolish := ans;
  30. end;
  31. end;
  32.  
  33. Procedure scambia (var c,b: longint);
  34. var x:longint;
  35. begin
  36. x:=c;
  37. c:=b;
  38. b:=x;
  39. end;
  40. Procedure ordinamento (estremoi,estremos: dword; var v : elenco; ordinato:boolean);
  41. var inf, sup, medio:dword;
  42. pivot :longint;
  43. begin
  44. inf:=estremoi;
  45. sup:=estremos;
  46. medio:= (estremoi+estremos) div 2;
  47. pivot:=v[medio];
  48. repeat
  49. if (ordinato) then
  50. begin
  51. while (v[inf]<pivot) do inf:=inf+1;
  52. while (v[sup]>pivot) do sup:=sup-1;
  53. end;
  54. if inf<=sup then
  55. begin
  56. scambia(v[inf],v[sup]);
  57. inf:=inf+1;
  58. sup:=sup-1;
  59. end;
  60. until inf>sup;
  61. if (estremoi<sup) then ordinamento(estremoi,sup,v,ordinato);
  62. if (inf<estremos) then ordinamento(inf,estremos,v,ordinato);
  63. end;
  64.  
  65. begin
  66. {
  67.   uncomment the following lines if you want to read/write from files
  68.   assign(input, 'input.txt'); reset(input);
  69.   assign(output, 'output.txt'); rewrite(output);
  70. }
  71. readln( N, K, L);
  72. for i:=0 to N-1 do
  73. read(A[i]);
  74. readln;
  75. for i := 0 to N-1 do
  76. if (A[i] >= K) then A[i] := A[i]-K;
  77.  
  78. writeln(demolish(N, K, L, A));
  79.  
  80. end.
  81.  
Success #stdin #stdout 0s 5316KB
stdin
1 6 1
0 
stdout
0