Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long LL;
- const int maxn = 1000000 + 100;
- const int Log = 20;
- int T, n;
- int a[maxn], b[maxn];
- int stMax[maxn][Log], mn[maxn];
- void init() {
- mn[0] = -1;
- for(int i = 1; i <= n; ++i) {
- mn[i] = ((i & (i - 1)) == 0)? mn[i - 1] + 1: mn[i - 1];
- stMax[i][0] = a[i];
- }
- for(int j = 1; j <= mn[n]; ++j) {
- for(int i = 1; i + (1 << j) - 1 <= n; ++i) {
- stMax[i][j] = max(stMax[i][j - 1], stMax[i + (1 << (j - 1))][j - 1]);
- }
- }
- }
- int rmqMax(int L, int R) {
- int k = mn[R - L + 1];
- return max(stMax[L][k], stMax[R - (1 << k) + 1][k]);
- }
- bool judge(LL x) {
- int idx = 1;
- LL leftCoin = 0;
- int bTmp = b[1];
- for (int i = 1; i <= n; ++i) {
- LL xTmp = x;
- if (xTmp <= leftCoin) {
- leftCoin -= xTmp;
- continue;
- }
- xTmp -= leftCoin;
- leftCoin = 0;
- while (idx <= i && xTmp > 0) {
- LL mx = rmqMax(idx, i);
- LL cut = min((xTmp + mx - 1) / mx, (LL) bTmp);
- leftCoin += cut * mx;
- bTmp -= cut;
- if (bTmp == 0) {
- ++idx;
- bTmp = b[idx];
- }
- if (leftCoin <= xTmp) {
- xTmp -= leftCoin;
- leftCoin = 0;
- } else {
- leftCoin -= xTmp;
- xTmp = 0;
- }
- }
- if (xTmp > 0) {
- return false;
- }
- }
- return true;
- }
- int main() {
- #ifdef ExRoc
- freopen("test.txt", "r", stdin);
- #endif // ExRoc
- ios::sync_with_stdio(false);
- cin >> T;
- while (T--) {
- cin >> n;
- LL low = 0;
- LL high = 0;
- LL mid;
- for (int i = 1; i <= n; ++i) {
- cin >> a[i];
- }
- init();
- LL mx = rmqMax(1, n);
- for (int i = 1; i <= n; ++i) {
- cin >> b[i];
- high += mx * b[i];
- }
- high /= n;
- ++high;
- while (high - low > 1) {
- mid = (high + low) >> 1;
- if (judge(mid)) {
- low = mid;
- } else {
- high = mid;
- }
- }
- cout << low << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement