Advertisement
a53

Buldo

a53
Mar 11th, 2020
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. ifstream f("buldo.in");
  5. ofstream g("buldo.out");
  6. const int N = 100010;
  7. const int oo = 2000000000;
  8. int n,h[N],c[N],v[N],m;
  9. int main()
  10. {
  11. f>>n;
  12. for(int i=1;i<=n;i++)
  13. f>>h[i];
  14. v[m]=oo;
  15. c[m]=0;
  16. for(int i=n;i>=1;i--)
  17. {
  18. if(h[i]<v[m])
  19. {
  20. m++;v[m]=h[i];c[m]=1;
  21. continue;
  22.  
  23. }
  24. int r=h[i]-v[m];c[m]++;
  25. /// cat timp am suficient sa ridic ultimul nivel la penultimul nivel
  26. do
  27. {
  28. int64_t need = 1LL*c[m]*(v[m-1]-v[m]);
  29. if(need<=1LL*r)
  30. {
  31. c[m-1]+=c[m];
  32. r-=need;
  33. m--;
  34. continue;
  35. }
  36. int hplus=r/c[m];
  37. v[m]+=hplus;
  38. r=r%c[m];
  39. if(r==0)
  40. break;
  41. m++;
  42. v[m]=v[m-1];
  43. v[m-1]++;
  44. c[m]=c[m-1]-r;
  45. c[m-1]=r;
  46. break;
  47.  
  48. }
  49. while(1);
  50. }
  51. g<<v[m];
  52. return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement