#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
travel.cpp: In function 'int32_t main()':
travel.cpp:27:9: warning: unused variable 'ant' [-Wunused-variable]
27 | int ant = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
26 ms |
8276 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
26 ms |
8276 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
436 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Execution timed out |
3115 ms |
362688 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
26 ms |
8276 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |