Submission #1102763

# Submission time Handle Problem Language Result Execution time Memory
1102763 2024-10-18T19:07:18 Z PagodePaiva Bitaro's travel (JOI23_travel) C++17
0 / 100
3000 ms 367616 KB
#include<bits/stdc++.h>
#define int long long

using namespace std;

map <array <int, 3>, int> dp;

const int N = 200010;
int v[N];

int solve(int l, int r, int flag){
    //cout << l << ' ' << r << ' ' << flag << '\n';
    if(dp.find({l, r, flag}) != dp.end()) return dp[{l, r, flag}];
    if(flag == 0){
        if(v[l]-v[l-1] <= v[r+1]-v[r]) return dp[{l, r, flag}] = solve(l-1, r, flag) + v[l]-v[l-1];
        else return dp[{l, r, flag}] = solve(l, r+1, 1) + v[r+1]-v[l];
    }
    else{
        if(v[r+1]-v[r] < v[r]-v[l-1]) return dp[{l, r, flag}] = solve(l, r+1, flag)+v[r+1]-v[r];
        else return dp[{l, r, flag}] = solve(l-1, r, 0) + v[r]-v[l-1];
    }
}

int32_t main(){
    int n;
    cin >> n;
    v[0] = -3e9;
    v[n+1] = 3e9;
    dp[{1, n, 0}] = dp[{1, n, 1}] = 0;
    for(int i = 1;i <= n;i++){
        cin >> v[i];
    }
    for(int i = 1;i <= n;i++){
        solve(i, i, 0);
    }
    int q;
    cin >> q;
    while(q--){
        int x;
        cin >> x;
        int l = 1, r = n;
        int ans = 0;
        while(l <= r){
            int mid = (l+r)/2;
            if(v[mid] >= x){
                ans = mid;
                r = mid-1;
            }
            else{
                l = mid+1;
            }
        }
        if(ans == 0) ans = n+1;
        if(v[ans]-x > x-v[ans-1]) cout << dp[{ans-1, ans-1, 0}] + x-v[ans-1] << '\n';
        else cout << dp[{ans, ans, 0}]+v[ans]-x << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Incorrect 23 ms 7268 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Incorrect 23 ms 7268 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Execution timed out 3116 ms 367616 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Incorrect 23 ms 7268 KB Output isn't correct
3 Halted 0 ms 0 KB -