Advertisement
rajeshinternshala

Untitled

Jan 13th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. bool check(long long mid, vector<int> &v, int m)
  2. {
  3.     int c = 1;
  4.     long long sm = 0;
  5.     for (int i = 0; i < v.size(); i++)
  6.     {
  7.         sm += 1LL * v[i];
  8.         if (sm > mid)
  9.         {
  10.             sm = v[i];
  11.             c++;
  12.         }
  13.     }
  14.     return c <= m;
  15. }
  16. int help(int n, vector<int> &b)
  17. {
  18.     long long m = n;
  19.     n = b.size();
  20.     long long sm = 0;
  21.     int mx = 0;
  22.     for (auto i : b)
  23.     {
  24.         sm += 1LL*i;
  25.         mx = max(mx, i);
  26.     }
  27.     long long l = mx, r = sm;
  28.     long long ans = r;
  29.     while (l <= r)
  30.     {
  31.         long long mid = (l + r) / 2;
  32.         if (check(mid, b, m))
  33.         {
  34.             ans = mid;
  35.             r = mid - 1;
  36.         }
  37.         else
  38.             l = mid + 1;
  39.     }
  40.     return ans;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement