답안 #890888

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
890888 2023-12-22T04:54:30 Z vjudge1 Bitaro's travel (JOI23_travel) C++17
45 / 100
106 ms 11596 KB
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long

using i64 = long long;

template <class F, class _S>
bool chmin(F &u, const _S &v){
    bool flag = false;
    if ( u > v ){
        u = v; flag |= true;
    }
    return flag;
}

template <class F, class _S>
bool chmax(F &u, const _S &v){
    bool flag = false;
    if ( u < v ){
        u = v; flag |= true;
    }
    return flag;
}

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

    int n; cin >> n;
    vector <int> X(n);
    for ( auto &u: X ) cin >> u;
    int q; cin >> q;
    vector <int> S(q);
    for ( auto &u: S ) cin >> u;
    if ( q == 1 ){
        int x = S[0];
        const int inf = 1e15;
        set <int> st{-inf, inf};
        for ( int i = 0; i < n; i++ ){
            st.insert(X[i]);
        }
        int ans = 0;
        while ( st.size() > 2 ){
            auto it = st.lower_bound(x);
            assert(*prev(it) <= x && *it >= x);
            if ( abs(*prev(it) - x) <= abs(*it - x) ){
                ans += x - *prev(it);
                x = *prev(it);
                st.erase(prev(it));
            } else{
                ans += -x + *it;
                x = *it;
                st.erase(it);
            }
        }
        cout << ans << ln;
        return 0;
    }
    for ( auto &xx: S ){
        int it = lower_bound(all(X), xx) - X.begin();
        if ( it - 1 >= 0 && (it == n || X[it] - xx >= xx - X[it - 1]) ) --it;
        int ans = abs(X[it] - xx), l = it - 1, r = it + 1, x = X[it];
        while ( l >= 0 || r < n ){
//            cout << l + 1 << " " << r + 1 << " -> " << x << ", " << ans << ln;
            if ( l < 0 ){
                ans += abs(X.back() - x);
                break;
            } else if ( r >= n ){
                ans += abs(X[0] - x);
                break;
            } else{
                if ( abs(X[r] - x) >= 100 && abs(X[l] - x) <= 100 ){
                    ans += abs(X[0] - x);
                    x = X[0]; l = -1;
                    continue;
                }
                if ( abs(X[l] - x) > 100  ){
                    ans += abs(X.back() - x);
                    x = X.back(); r = n;
                    continue;
                }
                if ( abs(X[l] - x) <= abs(X[r] - x) ){
                    ans += abs(X[l] - x);
                    x = X[l--];
                } else{
                    ans += abs(X[r] - x);
                    x = X[r++];
                }
            }
        }
        cout << ans << ln;
    }

    cout << '\n';
}

# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 62 ms 11340 KB Output is correct
18 Correct 63 ms 11268 KB Output is correct
19 Correct 60 ms 11356 KB Output is correct
20 Correct 81 ms 11224 KB Output is correct
21 Correct 59 ms 11244 KB Output is correct
22 Correct 74 ms 11348 KB Output is correct
23 Correct 65 ms 11348 KB Output is correct
24 Correct 59 ms 11348 KB Output is correct
25 Correct 60 ms 11272 KB Output is correct
26 Correct 63 ms 11212 KB Output is correct
27 Correct 73 ms 11596 KB Output is correct
28 Correct 63 ms 11352 KB Output is correct
29 Correct 59 ms 11216 KB Output is correct
30 Correct 59 ms 11252 KB Output is correct
31 Correct 59 ms 11244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 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 344 KB Output is correct
7 Correct 91 ms 4944 KB Output is correct
8 Correct 104 ms 4820 KB Output is correct
9 Correct 96 ms 4944 KB Output is correct
10 Correct 106 ms 4972 KB Output is correct
11 Correct 93 ms 4892 KB Output is correct
12 Correct 92 ms 4844 KB Output is correct
13 Correct 29 ms 3932 KB Output is correct
14 Correct 22 ms 2396 KB Output is correct
15 Correct 56 ms 5212 KB Output is correct
16 Correct 55 ms 5200 KB Output is correct
17 Correct 65 ms 5356 KB Output is correct
18 Correct 46 ms 3752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 62 ms 11340 KB Output is correct
18 Correct 63 ms 11268 KB Output is correct
19 Correct 60 ms 11356 KB Output is correct
20 Correct 81 ms 11224 KB Output is correct
21 Correct 59 ms 11244 KB Output is correct
22 Correct 74 ms 11348 KB Output is correct
23 Correct 65 ms 11348 KB Output is correct
24 Correct 59 ms 11348 KB Output is correct
25 Correct 60 ms 11272 KB Output is correct
26 Correct 63 ms 11212 KB Output is correct
27 Correct 73 ms 11596 KB Output is correct
28 Correct 63 ms 11352 KB Output is correct
29 Correct 59 ms 11216 KB Output is correct
30 Correct 59 ms 11252 KB Output is correct
31 Correct 59 ms 11244 KB Output is correct
32 Correct 1 ms 600 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 344 KB Output is correct
38 Correct 91 ms 4944 KB Output is correct
39 Correct 104 ms 4820 KB Output is correct
40 Correct 96 ms 4944 KB Output is correct
41 Correct 106 ms 4972 KB Output is correct
42 Correct 93 ms 4892 KB Output is correct
43 Correct 92 ms 4844 KB Output is correct
44 Correct 29 ms 3932 KB Output is correct
45 Correct 22 ms 2396 KB Output is correct
46 Correct 56 ms 5212 KB Output is correct
47 Correct 55 ms 5200 KB Output is correct
48 Correct 65 ms 5356 KB Output is correct
49 Correct 46 ms 3752 KB Output is correct
50 Incorrect 84 ms 9652 KB Output isn't correct
51 Halted 0 ms 0 KB -