Submission #964040

# Submission time Handle Problem Language Result Execution time Memory
964040 2024-04-16T08:46:35 Z RaresFelix Bitaro's travel (JOI23_travel) C++17
100 / 100
183 ms 70708 KB
#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC target("avx,avx2,fma")

#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
#define int ll 
using ll = long long;
using ld = long double;  // or double, if TL is tight
using str = string; 
using ii = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;

const int INF = 1e9;

struct RMQmax {
    int n;
    vector<vi> M;

    RMQmax(vi V) {
        n = V.size();
        M.push_back(V);
        for(int k = 1; (1 << k) <= n; ++k) {
            M.push_back(M.back());
            for(int i = 0; i < n; ++i)
                if(i + (1 << (k - 1)) < n)
                    M[k][i] = max(M[k][i], M[k - 1][i + (1 << (k - 1))]);
        }
    }

    int query(int p, int k) {
        return M[k][p];
    }
};

struct RMQmin {
    int n;
    vector<vi> M;

    RMQmin(vi V) {
        n = V.size();
        M.push_back(V);
        for(int k = 1; (1 << k) <= n; ++k) {
            M.push_back(M.back());
            for(int i = 0; i < n; ++i)
                if(i + (1 << (k - 1)) < n)
                    M[k][i] = min(M[k][i], M[k - 1][i + (1 << (k - 1))]);
        }
    }

    int query(int p, int k) {
        return M[k][p];
    }
};

signed main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    ///TODO long long

    int n;
    cin >> n;

    vi V(n), Dif1(n), Dif2(n), DP(n, 0);

    for(int i = 0; i < n; ++i)
        cin >> V[i];

    for(int i = 0; i + 1 < n; ++i) Dif1[i] = 2 * V[i] - V[i + 1];
    Dif1[n - 1] = INF;
    RMQmin QD1(Dif1);
    
    for(int i = 1; i < n; ++i) Dif2[i] = 2 * V[i] - V[i - 1];
    Dif2[0] = INF;
    RMQmax QD2(Dif2);

    for(int i = 0; i < n; ++i) {
        DP[i] = 0;
        int st = i, dr = i, dir = 0;
        ///dir: 0 pt st, 1 pt dr
        if(i == 0 || i == n - 1) {
            dir = !i;
        } else {
            if(V[i] - V[i - 1] <= V[i + 1] - V[i]) dir = 0;
            else dir = 1;
        }
        while(st != 0 || dr != n - 1) {
            if((!dir && st == 0) || (dir && dr == n - 1)) {
                DP[i] += V[dr] - V[st];
                dir ^= 1;
            }
            if(dir) {
                if(st == 0) {
                    DP[i] += V.back() - V[dr];
                    dr = n - 1;
                    break;
                }
                int dr0 = dr;
                for(int k = (int)QD1.M.size() - 1; k >= 0; --k) {
                    if(dr + (1 << k) >= n) continue;
                    if(QD1.query(dr, k) > V[st - 1]) {
                        dr += (1 << k);
                    } 
                }
                DP[i] += V[dr] - V[dr0];
                dir ^= 1;
                DP[i] += V[dr] - V[st];
            } else {
                if(dr == n - 1) {
                    DP[i] += V[st] - V[0];
                    st = 0;
                    break;
                }
                int st0 = st;
                for(int k = (int)QD2.M.size() - 1; k >= 0; --k) {
                    if(st - (1 << k) < 0) continue;
                    if(QD2.query(st - (1 << k) + 1, k) <= V[dr + 1])
                        st -= (1 << k);
                }
                DP[i] += V[st0] - V[st];
                dir ^= 1;
                DP[i] += V[dr] - V[st];
            }
        }
    }
    int q;
    cin >> q;
    for(int i = 0; i < q; ++i) {
        int p;
        cin >> p;
        int st = 0, dr = n - 1, mij;
        while(st < dr) {
            mij = (st + dr + 1) / 2;
            if(V[mij] > p) dr = mij - 1;
            else st = mij;
        }
        int id = 0, cre = 2e9;
        for(int j = max(st - 1, 0ll); j <= min(n - 1, st + 1); ++j)
            if(abs(V[j] - p) < cre) {
                cre = abs(V[j] - p);
                id = j;
            }
        cout << cre + DP[id] << "\n";
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 708 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 796 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 2 ms 860 KB Output is correct
6 Correct 2 ms 860 KB Output is correct
7 Correct 1 ms 856 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 452 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 728 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 708 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 796 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 2 ms 860 KB Output is correct
6 Correct 2 ms 860 KB Output is correct
7 Correct 1 ms 856 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 452 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 728 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 92 ms 66616 KB Output is correct
18 Correct 87 ms 66616 KB Output is correct
19 Correct 101 ms 66360 KB Output is correct
20 Correct 94 ms 66612 KB Output is correct
21 Correct 97 ms 66760 KB Output is correct
22 Correct 93 ms 66360 KB Output is correct
23 Correct 90 ms 66628 KB Output is correct
24 Correct 85 ms 66612 KB Output is correct
25 Correct 81 ms 66612 KB Output is correct
26 Correct 81 ms 66448 KB Output is correct
27 Correct 85 ms 66612 KB Output is correct
28 Correct 85 ms 66360 KB Output is correct
29 Correct 152 ms 66564 KB Output is correct
30 Correct 139 ms 66360 KB Output is correct
31 Correct 153 ms 66564 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 456 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 104 ms 68512 KB Output is correct
8 Correct 111 ms 68548 KB Output is correct
9 Correct 115 ms 68408 KB Output is correct
10 Correct 106 ms 68364 KB Output is correct
11 Correct 107 ms 68368 KB Output is correct
12 Correct 114 ms 68388 KB Output is correct
13 Correct 30 ms 4188 KB Output is correct
14 Correct 23 ms 1628 KB Output is correct
15 Correct 144 ms 69428 KB Output is correct
16 Correct 128 ms 69432 KB Output is correct
17 Correct 123 ms 69676 KB Output is correct
18 Correct 33 ms 4184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 708 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 796 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 2 ms 860 KB Output is correct
6 Correct 2 ms 860 KB Output is correct
7 Correct 1 ms 856 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 452 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 728 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 92 ms 66616 KB Output is correct
18 Correct 87 ms 66616 KB Output is correct
19 Correct 101 ms 66360 KB Output is correct
20 Correct 94 ms 66612 KB Output is correct
21 Correct 97 ms 66760 KB Output is correct
22 Correct 93 ms 66360 KB Output is correct
23 Correct 90 ms 66628 KB Output is correct
24 Correct 85 ms 66612 KB Output is correct
25 Correct 81 ms 66612 KB Output is correct
26 Correct 81 ms 66448 KB Output is correct
27 Correct 85 ms 66612 KB Output is correct
28 Correct 85 ms 66360 KB Output is correct
29 Correct 152 ms 66564 KB Output is correct
30 Correct 139 ms 66360 KB Output is correct
31 Correct 153 ms 66564 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 456 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 104 ms 68512 KB Output is correct
39 Correct 111 ms 68548 KB Output is correct
40 Correct 115 ms 68408 KB Output is correct
41 Correct 106 ms 68364 KB Output is correct
42 Correct 107 ms 68368 KB Output is correct
43 Correct 114 ms 68388 KB Output is correct
44 Correct 30 ms 4188 KB Output is correct
45 Correct 23 ms 1628 KB Output is correct
46 Correct 144 ms 69428 KB Output is correct
47 Correct 128 ms 69432 KB Output is correct
48 Correct 123 ms 69676 KB Output is correct
49 Correct 33 ms 4184 KB Output is correct
50 Correct 152 ms 70684 KB Output is correct
51 Correct 151 ms 70568 KB Output is correct
52 Correct 154 ms 70680 KB Output is correct
53 Correct 142 ms 70676 KB Output is correct
54 Correct 142 ms 70676 KB Output is correct
55 Correct 139 ms 70684 KB Output is correct
56 Correct 183 ms 70708 KB Output is correct
57 Correct 174 ms 70416 KB Output is correct
58 Correct 176 ms 70456 KB Output is correct