This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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, flag) + v[r]-v[l-1];
}
}
int32_t main(){
int n;
cin >> n;
int ant = 0;
v[0] = -1e18;
v[n+1] = 1e18;
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;
}
Compilation message (stderr)
travel.cpp: In function 'int32_t main()':
travel.cpp:27:9: warning: unused variable 'ant' [-Wunused-variable]
27 | int ant = 0;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |