Advertisement
Dmaxiya

扶苏出勤日记 参考代码

Apr 1st, 2025 (edited)
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = 1000000 + 100;
  6. const int Log = 20;
  7. int T, n;
  8. int a[maxn], b[maxn];
  9. int stMax[maxn][Log], mn[maxn];
  10.  
  11. void init() {
  12.     mn[0] = -1;
  13.     for(int i = 1; i <= n; ++i) {
  14.         mn[i] = ((i & (i - 1)) == 0)? mn[i - 1] + 1: mn[i - 1];
  15.         stMax[i][0] = a[i];
  16.     }
  17.     for(int j = 1; j <= mn[n]; ++j) {
  18.         for(int i = 1; i + (1 << j) - 1 <= n; ++i) {
  19.             stMax[i][j] = max(stMax[i][j - 1], stMax[i + (1 << (j - 1))][j - 1]);
  20.         }
  21.     }
  22. }
  23.  
  24. int rmqMax(int L, int R) {
  25.     int k = mn[R - L + 1];
  26.     return max(stMax[L][k], stMax[R - (1 << k) + 1][k]);
  27. }
  28.  
  29. bool judge(LL x) {
  30.     int idx = 1;
  31.     LL leftCoin = 0;
  32.     int bTmp = b[1];
  33.     for (int i = 1; i <= n; ++i) {
  34.         LL xTmp = x;
  35.         if (xTmp <= leftCoin) {
  36.             leftCoin -= xTmp;
  37.             continue;
  38.         }
  39.         xTmp -= leftCoin;
  40.         leftCoin = 0;
  41.         while (idx <= i && xTmp > 0) {
  42.             LL mx = rmqMax(idx, i);
  43.             LL cut = min((xTmp + mx - 1) / mx, (LL) bTmp);
  44.             leftCoin += cut * mx;
  45.             bTmp -= cut;
  46.             if (bTmp == 0) {
  47.                 ++idx;
  48.                 bTmp = b[idx];
  49.             }
  50.             if (leftCoin <= xTmp) {
  51.                 xTmp -= leftCoin;
  52.                 leftCoin = 0;
  53.             } else {
  54.                 leftCoin -= xTmp;
  55.                 xTmp = 0;
  56.             }
  57.         }
  58.         if (xTmp > 0) {
  59.             return false;
  60.         }
  61.     }
  62.     return true;
  63. }
  64.  
  65. int main() {
  66. #ifdef ExRoc
  67.     freopen("test.txt", "r", stdin);
  68. #endif // ExRoc
  69.     ios::sync_with_stdio(false);
  70.  
  71.     cin >> T;
  72.     while (T--) {
  73.         cin >> n;
  74.         LL low = 0;
  75.         LL high = 0;
  76.         LL mid;
  77.         for (int i = 1; i <= n; ++i) {
  78.             cin >> a[i];
  79.         }
  80.         init();
  81.         LL mx = rmqMax(1, n);
  82.         for (int i = 1; i <= n; ++i) {
  83.             cin >> b[i];
  84.             high += mx * b[i];
  85.         }
  86.         high /= n;
  87.         ++high;
  88.         while (high - low > 1) {
  89.             mid = (high + low) >> 1;
  90.             if (judge(mid)) {
  91.                 low = mid;
  92.             } else {
  93.                 high = mid;
  94.             }
  95.         }
  96.         cout << low << endl;
  97.     }
  98.  
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement