Submission #979020

# Submission time Handle Problem Language Result Execution time Memory
979020 2024-05-10T06:19:06 Z abc864197532 Bitaro's travel (JOI23_travel) C++17
100 / 100
182 ms 22172 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(x) x.begin(), x.end()
#define pii pair<int,int>
#define sz(a) (int(a.size()))
const int N = 105;

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int n;
    cin >> n;
    vector <int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    if (n == 1) {
        int q;
        cin >> q;
        while (q--) {
            int x;
            cin >> x;
            cout << abs(a[0] - x) << '\n';
        }
        return 0;
    }
    vector <int> ptl(n), ptr(n);
    {
        vector <pair <int, int>> event;
        for (int i = 1; i + 1 < n; ++i) {
            event.emplace_back(2 * a[i] - a[i - 1], -i);
            event.emplace_back(a[i + 1], i);
        }
        sort(all(event)), reverse(all(event));
        set <int> S;
        for (auto [x, y] : event) {
            if (y > 0) {
                auto it = S.upper_bound(y);
                if (it != S.begin()) {
                    ptl[y] = *prev(it);
                }
            } else {
                S.insert(-y);
            }
        }
    }
    {
        vector <pair <int, int>> event;
        for (int i = 1; i + 1 < n; ++i) {
            event.emplace_back(2 * a[i] - a[i + 1], -i);
            event.emplace_back(a[i - 1], i);
        }
        sort(all(event));
        set <int> S;
        for (auto [x, y] : event) {
            if (y > 0) {
                auto it = S.lower_bound(y);
                if (it != S.end()) {
                    ptr[y] = *it;
                } else {
                    ptr[y] = n - 1;
                }
            } else {
                S.insert(-y);
            }
        }
    }
    vector <ll> ans(n);
    for (int st = 0; st < n; ++st) {
        int l = st, r = st;
        int left = st == n - 1 || (st && a[st] - a[st - 1] <= a[st + 1] - a[st]);
        while (l > 0 || r + 1 < n) {
            if (l == 0 || r + 1 == n) {
                ans[st] += a[n - 1] - a[0];
                break;
            }
            if (left) {
                // find max i s.t. 2a_i > a{r + 1} + a_{i - 1}
                int now = ptl[r];
                // while (now && 2 * a[now] <= a[r + 1] + a[now - 1]) {
                //     now--;
                // }
                if (!now) {
                    ans[st] += (a[l] - a[now]);
                    l = now;
                } else {
                    ans[st] += (a[l] - a[now]) + (a[r + 1] - a[now]);
                    left ^= 1;
                    l = now, r++;
                }
            } else {
                // find min i s.t. 2a_i <= a_{l - 1} + a_{i + 1} 
                int now = ptr[l];
                // while (now + 1 < n && 2 * a[now] > a[l - 1] + a[now + 1]) {
                //     now++;
                // }
                if (now == n - 1) {
                    ans[st] += (a[now] - a[r]);
                    r = now;
                } else {
                    ans[st] += (a[now] - a[r]) + (a[now] - a[l - 1]);
                    left ^= 1;
                    l--, r = now;
                }
            }
        }
    }
    int q;
    cin >> q;
    while (q--) {
        int x;
        cin >> x;
        int p = lower_bound(all(a), x) - a.begin();
        if (p < n && a[p] == x) {
            cout << ans[p] << '\n';
        } else {
            int q = p - 1;
            if (q == -1) {
                cout << ans[p] + abs(x - a[p]) << '\n';
            } else if (p == n) {
                cout << ans[q] + abs(x - a[q]) << '\n';
            } else if (x - a[q] <= a[p] - x) {
                cout << ans[q] + abs(x - a[q]) << '\n';
            } else {
                cout << ans[p] + abs(x - a[p]) << '\n';
            }
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 456 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 456 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 125 ms 17844 KB Output is correct
18 Correct 119 ms 17744 KB Output is correct
19 Correct 123 ms 17248 KB Output is correct
20 Correct 123 ms 17252 KB Output is correct
21 Correct 132 ms 17240 KB Output is correct
22 Correct 123 ms 18140 KB Output is correct
23 Correct 126 ms 18220 KB Output is correct
24 Correct 107 ms 17344 KB Output is correct
25 Correct 106 ms 18036 KB Output is correct
26 Correct 106 ms 17228 KB Output is correct
27 Correct 119 ms 17596 KB Output is correct
28 Correct 112 ms 18116 KB Output is correct
29 Correct 133 ms 18152 KB Output is correct
30 Correct 118 ms 18164 KB Output is correct
31 Correct 121 ms 18004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 170 ms 19200 KB Output is correct
8 Correct 169 ms 19536 KB Output is correct
9 Correct 169 ms 19784 KB Output is correct
10 Correct 171 ms 20052 KB Output is correct
11 Correct 168 ms 19068 KB Output is correct
12 Correct 169 ms 19256 KB Output is correct
13 Correct 30 ms 4188 KB Output is correct
14 Correct 21 ms 1624 KB Output is correct
15 Correct 149 ms 21116 KB Output is correct
16 Correct 142 ms 20304 KB Output is correct
17 Correct 144 ms 21072 KB Output is correct
18 Correct 30 ms 4188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 456 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 125 ms 17844 KB Output is correct
18 Correct 119 ms 17744 KB Output is correct
19 Correct 123 ms 17248 KB Output is correct
20 Correct 123 ms 17252 KB Output is correct
21 Correct 132 ms 17240 KB Output is correct
22 Correct 123 ms 18140 KB Output is correct
23 Correct 126 ms 18220 KB Output is correct
24 Correct 107 ms 17344 KB Output is correct
25 Correct 106 ms 18036 KB Output is correct
26 Correct 106 ms 17228 KB Output is correct
27 Correct 119 ms 17596 KB Output is correct
28 Correct 112 ms 18116 KB Output is correct
29 Correct 133 ms 18152 KB Output is correct
30 Correct 118 ms 18164 KB Output is correct
31 Correct 121 ms 18004 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 170 ms 19200 KB Output is correct
39 Correct 169 ms 19536 KB Output is correct
40 Correct 169 ms 19784 KB Output is correct
41 Correct 171 ms 20052 KB Output is correct
42 Correct 168 ms 19068 KB Output is correct
43 Correct 169 ms 19256 KB Output is correct
44 Correct 30 ms 4188 KB Output is correct
45 Correct 21 ms 1624 KB Output is correct
46 Correct 149 ms 21116 KB Output is correct
47 Correct 142 ms 20304 KB Output is correct
48 Correct 144 ms 21072 KB Output is correct
49 Correct 30 ms 4188 KB Output is correct
50 Correct 170 ms 21712 KB Output is correct
51 Correct 182 ms 21296 KB Output is correct
52 Correct 177 ms 21440 KB Output is correct
53 Correct 159 ms 22172 KB Output is correct
54 Correct 161 ms 21816 KB Output is correct
55 Correct 165 ms 21492 KB Output is correct
56 Correct 154 ms 22100 KB Output is correct
57 Correct 153 ms 22088 KB Output is correct
58 Correct 153 ms 21056 KB Output is correct