TL2.PAS 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const maxn=1000;
  2. var a:array[1..maxn] of longint;
  3. b:array[1..maxn] of integer;
  4. i,n,t,m,k:integer;
  5. tmp,f,ss,s:longint;
  6. procedure insert(i:integer);
  7. var j,tmp:integer;
  8. begin
  9. inc(t);b[t]:=i;j:=t;
  10. while (j>1) and (a[b[j]]>a[b[j div 2]]) do
  11. begin
  12. tmp:=b[j];b[j]:=b[j div 2];b[j div 2]:=tmp;j:=j div 2;
  13. end;
  14. end;
  15. function max:integer;
  16. var i,j,v,tmp:integer;
  17. begin
  18. v:=b[1];b[1]:=b[t];dec(t);i:=1;
  19. while i<=t div 2 do
  20. begin
  21. if (i*2=t) or (a[b[2*i]]>a[b[2*i+1]]) then
  22. j:=i*2
  23. else
  24. j:=i*2+1;
  25. if a[b[j]]>a[b[i]] then
  26. begin
  27. tmp:=b[i];b[i]:=b[j];b[j]:=tmp;i:=j;
  28. end
  29. else
  30. break;
  31. end;
  32. max:=v;
  33. end;
  34. begin
  35. read(n);ss:=0;
  36. for i:=1 to n do
  37. begin
  38. read(a[i]);
  39. ss:=ss+a[i];
  40. end;
  41. t:=0;f:=0;
  42. for i:=1 to n do insert(i);
  43. while true do
  44. begin
  45. m:=max;s:=ss-a[m];k:=b[1];
  46. if a[k]*2<=s then
  47. begin
  48. inc(f,m);break;
  49. end
  50. else
  51. begin
  52. a[m]:=2*a[k]-s;
  53. dec(ss,s-a[m]);
  54. insert(m);
  55. if k<m then
  56. inc(f,m-k)
  57. else
  58. inc(f,n-k+m);
  59. end;
  60. end;
  61. i:=f mod n;
  62. if i=0 then i:=n;
  63. writeln('The student ',i,' can guess his number at round ',f);
  64. end.