Submission #1314732

#TimeUsernameProblemLanguageResultExecution timeMemory
1314732wedonttalkanymoreBitaro's travel (JOI23_travel)C++20
5 / 100
15 ms3568 KiB
#include <bits/stdc++.h>
/*
    Checklist: 
    - Check statement: 
    - Check filename: 
    - Check test limit: 
    - Stresstest: 
*/
using namespace std;
using ll = long long;

#define int long long
#define pii pair<ll, ll>
#define fi first
#define se second

const ll N = 5e5 + 5, inf = 1e18, mod = 1e9 + 7, block = 320, lim = 19;

int n, a[N];
int q;
int s[N];
int pfs[N];

int solve(int x) {
    int ans = 0;

    int pos = lower_bound(a + 1, a + n + 1, x) - a;
    pos = min(pos, n);

    int now;
    int L, R;

    if (pos > 1 && abs(x - a[pos - 1]) <= abs(x - a[pos])) {
        ans += abs(x - a[pos - 1]);
        L = R = pos - 1;
        now = 1;
    } 
    else {
        ans += abs(x - a[pos]);
        L = R = pos;
        now = 2;
    }

    while (L > 1 && R < n) {
        if (now == 1) { // dang o L
            int val = a[R + 1] - a[L];
            int p = lower_bound(a + 1, a + n + 1, a[L] - val) - a;
            if (p < L) {
                ans += a[L] - a[p];
                L = p;
            } 
            else {
                ans += val;
                R++;
                now = 2;
            }
        } 
        else { 
            int val = a[R] - a[L - 1];
            int p = upper_bound(a + 1, a + n + 1, a[R] + val) - a - 1;
            if (p > R) {
                ans += a[p] - a[R];
                R = p;
            } 
            else {
                ans += val;
                L--;
                now = 1;
            }
        }
    }
    ans += a[n] - a[1];
    return ans;
}


signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    if (fopen(".inp", "r")) {
        freopen(".inp", "r", stdin);
        freopen(".out", "w", stdout);
    }
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 2; i <= n; i++) pfs[i] = pfs[i - 1] + a[i] - a[i - 1];
    cin >> q;
    for (int i = 1; i <= q; i++) {
        cin >> s[i];
        cout << solve(s[i]) << '\n';
    }
    return 0;
}

Compilation message (stderr)

travel.cpp: In function 'int main()':
travel.cpp:81:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |         freopen(".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
travel.cpp:82:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |         freopen(".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...