Submission #1336510

#TimeUsernameProblemLanguageResultExecution timeMemory
1336510yhkhooBikeparking (EGOI24_bikeparking)C++17
100 / 100
166 ms5128 KiB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 300005;
int n, sy, x[MAXN], y[MAXN], tx[MAXN], ty[MAXN];

inline int cmp(const int& t, const int&s){
    return (t < s) - (s < t);
}

int f(int d){
    memcpy(tx, x, n*sizeof(int));
    memcpy(ty, y, n*sizeof(int));
    int* xp = tx + n - 1, *yp = ty;
    int s = 0, ret = 0;
    while(s < d){
        int tk, cx = xp - tx, cy = yp - ty;
        if(s + min(*xp, *yp) >= d){
            tk = d-s;
            *xp -= tk;
            *yp -= tk;
        }
        else{
            if(*xp < *yp){
                tk = *xp;
                *yp -= tk;
                *xp = 0;
                xp--;
            }
            else if(*yp < *xp){
                tk = *yp;
                *xp -= tk;
                *yp = 0;
                yp++;
            }
            else{
                tk = *xp;
                *xp = 0;
                *yp = 0;
                xp--; yp++;
            }
        }
        s += tk;
        //ret += tk * cmp(cx, cy);
    }
    ret -= d;
    xp = tx;
    while(s < sy){
        int tk, cx = xp - tx, cy = yp - ty;
        if(s + min(*xp, *yp) >= sy){
            tk = sy-s;
            *xp -= tk;
            *yp -= tk;
        }
        else{
            if(*xp < *yp){
                tk = *xp;
                *yp -= tk;
                *xp = 0;
                xp++;
            }
            else if(*yp < *xp){
                tk = *yp;
                *xp -= tk;
                *yp = 0;
                yp++;
            }
            else{
                tk = *xp;
                *xp = 0;
                *yp = 0;
                xp++; yp++;
            }
        }
        s += tk;
        ret += tk * cmp(cx, cy);
    }
    return ret;
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n;
    int sx = 0;
    sy = 0;
    for(int i=0; i<n; i++){
        cin >> x[i];
    }
    for(int i=0; i<n; i++){
        cin >> y[i];
        sy += y[i];
    }
    for(int i=0; i<n; i++){
        if(sy == sx){
            x[i] = 0;
        }
        else if(sx + x[i] >= sy){
            x[i] = sy - sx;
            sx = sy;
        }
        else{
            sx += x[i];
        }
    }
    int l = 0, r = sy;
    while(r - l >= 3){
        int m1 = l + (r - l) / 3;
        int m2 = r - (r - l) / 3;
        int f1 = f(m1);
        int f2 = f(m2);
        if(f1 < f2){
            l = m1;
        }
        else if(f2 < f1){
            r = m2;
        }
        else{
            l = m1;
        }
    }
    int ans = INT_MIN;
    for(int i=l; i<=r; i++){
        ans = max(ans, f(i));
    }
    cout << ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...