fork download
  1. program sushi;
  2. const MAXN=100002;
  3. type piatto = record
  4. inizio:longint;
  5. durata:longint;
  6. peso:longint;
  7. fine:int64;
  8. end;
  9. elenco = array[0.. MAXN-1] of int64;
  10. table = array[0..MAXN-1] of piatto;
  11. var N,i,id:Longint;
  12. ricordafine, quantomangio, ricordoquantomangiato:int64;
  13. T : table;
  14. S,W,D,E, ricordaindice, ricordaindice2:elenco; (*array riferiti ai piatti Start, Weigth, Durata, End*)
  15. uscita : boolean;
  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: int64;
  52.  
  53. begin
  54. start:=0; eend:=N-1 ;
  55. repeat
  56. m:=(start + eend) div 2;
  57. if w[m]<target then start:=m+1
  58. else if w[m]>=target then begin id:=m; eend:=m-1; end;
  59. until start>eend;
  60. if start>N-1 then id:=-1;
  61. end;
  62.  
  63. begin
  64. (*assign(input, 'input.txt'); reset(input);
  65.   assign(output, 'output.txt'); rewrite(output);*)
  66. readln(N);
  67. for i:=0 to N-1 do begin ricordaindice[i]:=i; ricordaindice2[i]:=i; end;
  68. for i:=0 to N-1 do
  69. begin
  70. { dish on table }
  71. readln(S[i],W[i],D[i]);
  72. E[i]:=S[i]+D[i];
  73. T[i].inizio:=S[i];
  74. T[i].durata:=D[i];
  75. T[i].peso:=W[i];
  76. T[i].fine:=E[i];
  77. end;
  78. ordinamento(0,N-1,E, ricordaindice,true);
  79. ordinamento(0,N-1,S, ricordaindice2,true);
  80. ricordoquantomangiato:=0;
  81. for i:=0 to N-1 do
  82. begin
  83. quantomangio:=T[ricordaindice[i]].peso;
  84. ricordafine:=E[i];
  85. uscita:=false;
  86. while uscita=false do
  87. begin
  88. ricerca(S, ricordafine);
  89. if (ricordafine <=S[N-1]) and (T[ricordaindice2[id]].inizio>=ricordafine) then
  90. begin
  91. quantomangio:=quantomangio+T[ricordaindice2[id]].peso;
  92. ricordafine:=T[ricordaindice2[id]].inizio+T[ricordaindice2[id]].durata;
  93. if quantomangio>ricordoquantomangiato then ricordoquantomangiato:=quantomangio;
  94. end
  95. else uscita:=true;
  96. end;
  97. end;
  98. writeln(ricordoquantomangiato);
  99. end.
  100.  
Success #stdin #stdout 0.01s 5320KB
stdin
4
6 24 3
8 16 5
12 14 1
9 11 4

stdout
35