fork download
  1. program ricerca;
  2. const MAXN=100002;
  3. Type elenco = array[0.. MAXN-1] of int64;
  4.  
  5. var N,i, id:Longint;
  6. T : int64;
  7. P :elenco;
  8.  
  9. Procedure scambia (var a,b: int64);
  10. var x:int64;
  11. begin
  12. x:=a;
  13. a:=b;
  14. b:=x;
  15. end;
  16.  
  17. Procedure ordinamento (estremoi,estremos: int64; var v : elenco; ordinato:boolean);
  18. var inf, sup, medio:int64;
  19. pivot :int64;
  20. begin
  21. inf:=estremoi;
  22. sup:=estremos;
  23. medio:= (estremoi+estremos) div 2;
  24. pivot:=v[medio];
  25. repeat
  26. if (ordinato) then
  27. begin
  28. while (v[inf]<pivot) do inf:=inf+1;
  29. while (v[sup]>pivot) do sup:=sup-1;
  30. end;
  31. if inf<=sup then
  32. begin
  33. scambia(v[inf],v[sup]);
  34. inf:=inf+1;
  35. sup:=sup-1;
  36. end;
  37. until inf>sup;
  38. if (estremoi<sup) then ordinamento(estremoi,sup,v,ordinato);
  39. if (inf<estremos) then ordinamento(inf,estremos,v,ordinato);
  40. end;
  41. Procedure ricercaUpper (var w:elenco; target:int64); (*ritorna indice del valore maggiore/uguale a target oppure -1 se non esiste*)
  42. var m,start,eend: int64;
  43.  
  44. begin
  45. start:=0; eend:=N-1 ; m:=-1;
  46. while start<=eend do
  47. begin
  48. m:=(start + eend) div 2;
  49. if w[m]<target then start:=m+1
  50. else if w[m]>=target then begin id:=m; eend:=m-1 end;
  51. end;
  52. if start=N then id:=-1;
  53. end;
  54.  
  55. begin
  56. readln(N);
  57. readln (T); (*valore da confrontare*)
  58. for i:=0 to N-1 do read(P[i]); readln;
  59. ordinamento(0,N-1,P,true);
  60. for i:=0 to N-1 do write(P[i],' '); writeln;
  61. ricercaUpper(P, T);
  62. if id<>-1 then writeln(id)
  63. else writeln('target non trovato ') ;
  64. end.
Success #stdin #stdout 0s 5280KB
stdin
5
0
56 7 23 1 8
stdout
1 7 8 23 56 
0