제출 #1349292

#제출 시각아이디문제언어결과실행 시간메모리
1349292MisterReaperBitaro's travel (JOI23_travel)C++20
100 / 100
257 ms3420 KiB
#include <bits/stdc++.h>

using i64 = long long;

#ifdef DEBUG
    #include "debug.h"
#else
    #define debug(...) void(23)
#endif

constexpr i64 inf = i64(1E18);
constexpr int max_N = int(2E5) + 5;

int N;
int Q;
int X[max_N];

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin >> N;

    for (int i = 0; i < N; ++i) {
        std::cin >> X[i];
    }

    std::cin >> Q;
    while (Q--) {
        int S;
        std::cin >> S;
        int l = int(std::lower_bound(X, X + N, S) - X);
        int r = int(std::upper_bound(X, X + N, S) - X) - 1;
        int t = 0;
        i64 ans;
        if (l == r) {
            ans = 0;
        } else if (l == N) {
            ans = S - X[r];
            l = r;
        } else if (r == -1) {
            ans = X[l] - S;
            r = l;
        } else{
            if (X[l] - S < S - X[r]) {
                ans = X[l] - S;
                r = l;
            } else {
                ans = S - X[r];
                l = r;
            }
        }
        while (l != 0 && r != N - 1) {
            debug(l, r, t);
            if (X[r + 1] - (t == 0 ? X[l] : X[r]) < (t == 0 ? X[l] : X[r]) - X[l - 1]) {
                ans += X[r + 1] - (t == 0 ? X[l] : X[r]);
                r += 1;
                t = 1;
                int p = int(std::lower_bound(X + r, X + N, X[r] + (X[r] - X[l])) - X) - 1;
                ans += X[p] - X[r];
                r = p;
            } else {
                ans += (t == 0 ? X[l] : X[r]) - X[l - 1];
                l -= 1;
                t = 0;
                int p = int(std::lower_bound(X, X + l, X[l] - (X[r] - X[l])) - X);
                ans += X[l] - X[p];
                l = p;
            }
        }
        debug(l, r, t);
        if (l == 0) {
            ans += X[N - 1] - (t == 0 ? X[0] : X[r]);
        } else {
            ans += (t == 0 ? X[l] : X[N - 1]) - X[0];
        }
        std::cout << ans << '\n';
    }

    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...