Advertisement
pb_jiang

LC2528

Jan 10th, 2023
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. class Solution {
  2.     using ll = long long;
  3.     bool check(const vector<int>& st, ll pcnt, int r, int k) {
  4.         int span = r * 2 + 1;
  5.         int n = st.size();
  6.         vector<ll> diff(n + 1);
  7.         for (int i = 0; i < n; ++i) {
  8.             int lb = max(0, i - r);
  9.             int ub = min(n, i + r + 1);
  10.             diff[lb] += st[i];
  11.             diff[ub] -= st[i];
  12.         }
  13.        
  14.         ll acc = 0, cost = 0;
  15.         for (int i = 0; i < n; ++i) {
  16.             acc += diff[i];
  17.             if (acc < pcnt) {
  18.                 ll delta = pcnt - acc;
  19.                 diff[i] += delta;
  20.                 acc += delta;
  21.                
  22.                 int ub = min(i + span, n);
  23.                 diff[ub] -= delta;
  24.                 cost += delta;
  25.                
  26.                 if (cost > k) return false;
  27.             }
  28.         }
  29.         return cost <= k;
  30.     }
  31. public:
  32.     long long maxPower(vector<int>& stations, int r, int k) {
  33.         int span = 2 * r + 1;
  34.         ll lb = 0, cnt = 1e15, it, step;
  35.         while(cnt) {
  36.             it = lb, step = cnt / 2, it += step;
  37.             if (check(stations, it, r, k)) {
  38.                 lb = it + 1;
  39.                 cnt -= step + 1;
  40.             } else cnt = step;
  41.         }
  42.         return lb - 1;
  43.     }
  44. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement