Submission #1314727

#TimeUsernameProblemLanguageResultExecution timeMemory
1314727wedonttalkanymoreBitaro's travel (JOI23_travel)C++20
0 / 100
0 ms332 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;
    auto pos = upper_bound(a + 1, a + n + 1, x) - a - 1;
    int now = 0;
    int L, R;
    if (abs(x - a[pos]) <= abs(x - a[pos + 1])) {
        ans += abs(x - a[pos]);
        L = pos, R = pos;
        now = 1;
    }
    else {
        ans += abs(x - a[pos + 1]);
        L = pos + 1, R = pos + 1;
        now = 2;
    }
    while(L > 1 && R < n) {
        if (now == 1) { // dang xet (l - 1, l)
            int val = a[R + 1] - a[L];
            auto pos = lower_bound(a + 1, a + n + 1, a[L] - val) - a;
            if (pos < L) {
                ans += a[L] - a[pos];
                L = pos;
            }
            else {
                ans += val;
                R++;
                now = 2;
            }
        }
        else {
            int val = a[R] - a[L - 1];
            int pos = lower_bound(a + 1, a + n + 1, a[R] + val) - a;
            if (pos > R) {
                ans += a[pos] - a[R];
                R = pos;
            }
            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:75:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |         freopen(".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
travel.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |         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...