fork download
  1. program sushi;
  2. Uses Math;
  3. const MAXN=100002;
  4. type piatto = record
  5. inizio:longint;
  6. durata:longint;
  7. peso:longint;
  8. fine:int64;
  9. end;
  10. elenco = array[0.. MAXN-1] of int64;
  11. table = array[0..MAXN-1] of piatto;
  12. var N,i, id:Longint;
  13. ricordafine, quantomangio:int64;
  14. T : table;
  15. S,W,D,E, ricordaindice, ricordaindice2:elenco; (*array riferiti ai piatti Start, Weigth, Durata, End*)
  16.  
  17. Procedure scambia (var a,b: int64);
  18. var x:int64;
  19. begin
  20. x:=a;
  21. a:=b;
  22. b:=x;
  23. end;
  24.  
  25. Procedure ordinamento (estremoi,estremos: int64; var v : elenco; var u:elenco; ordinato:boolean);
  26. var inf, sup, medio:int64;
  27. pivot :int64;
  28. begin
  29. inf:=estremoi;
  30. sup:=estremos;
  31. medio:= (estremoi+estremos) div 2;
  32. pivot:=v[medio];
  33. repeat
  34. if (ordinato) then
  35. begin
  36. while (v[inf]<pivot) do inf:=inf+1;
  37. while (v[sup]>pivot) do sup:=sup-1;
  38. end;
  39. if inf<=sup then
  40. begin
  41. scambia(v[inf],v[sup]);
  42. scambia(u[inf],u[sup]);
  43. inf:=inf+1;
  44. sup:=sup-1;
  45. end;
  46. until inf>sup;
  47. if (estremoi<sup) then ordinamento(estremoi,sup,v,u,ordinato);
  48. if (inf<estremos) then ordinamento(inf,estremos,v,u,ordinato);
  49. end;
  50. Procedure ricerca (var w:elenco; target:int64);
  51. var m,start,eend: qword;
  52.  
  53. begin
  54. start:=0; eend:=N ;
  55. writeln (target);
  56. repeat
  57. m:=(start + eend) div 2;
  58. if w[m]<target then start:=m+1
  59. else if w[m]>=target then begin id:=m; eend:=m-1; end;
  60.  
  61. until start>eend;
  62.  
  63. end;
  64.  
  65. begin
  66. (*assign(input, 'input.txt'); reset(input);
  67.   assign(output, 'output.txt'); rewrite(output);*)
  68. readln(N);
  69. for i:=0 to N-1 do begin ricordaindice[i]:=i; ricordaindice2[i]:=i; end;
  70. for i:=0 to N-1 do
  71. begin
  72. { dish on table }
  73. readln(S[i],W[i],D[i]);
  74. E[i]:=S[i]+D[i];
  75. T[i].inizio:=S[i];
  76. T[i].durata:=D[i];
  77. T[i].peso:=W[i];
  78. T[i].fine:=E[i];
  79. end;
  80. ordinamento(0,N-1,E, ricordaindice,true);
  81. ordinamento(0,N-1,S, ricordaindice2,true);
  82. quantomangio:=0;
  83. for i:=0 to N-1 do write(S[i],' ', ricordaindice2[i],' '); writeln;
  84. for i:=0 to N-1 do write(E[i],' ', ricordaindice[i],' '); writeln;
  85. for i:=0 to N-1 do
  86. begin
  87. ricerca(S, E[i]);
  88. writeln('indice trov ',id);
  89. writeln('peso ',T[ricordaindice2[id]].peso);
  90. if id <>-1 then quantomangio:=quantomangio+T[ricordaindice2[id]].peso else continue;
  91. end;
  92. writeln(quantomangio);
  93. end.
Success #stdin #stdout 0.01s 5320KB
stdin
3
1 5 3
4 3 6
0 7 5
stdout
0 2 1 0 4 1 
4 0 5 2 10 1 
4
indice trov 2
peso 3
5
indice trov 2
peso 3
10
indice trov 2
peso 3
9