#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
using namespace std;
const int NS = (int)2e5 + 4;
int n, q;
int a[NS];
unordered_map<int, int> dp[NS][2];
int sol(int l, int r, int dir){
if(l == 0 && r == n - 1) return 0;
if(dp[l][dir][r]) return dp[l][dir][r];
int rv = 0;
if(!dir){
if(l && (r == n - 1 || a[l] - a[l - 1] <= a[r + 1] - a[l])){
rv = a[l] - a[l - 1] + sol(l - 1, r, 0);
}
else{
rv = a[r + 1] - a[l] + sol(l, r + 1, 1);
}
}
else{
if(l && (r == n - 1 || a[r] - a[l - 1] <= a[r + 1] - a[r])){
rv = a[r] - a[l - 1] + sol(l - 1, r, 0);
}
else{
rv = a[r + 1] - a[r] + sol(l, r + 1, 1);
}
}
return (dp[l][dir][r] = rv);
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 0; i < n; ++i){
cin >> a[i];
}
cin >> q;
while(q--){
int x;
cin >> x;
int p = lower_bound(a, a + n, x) - a;
if(p == n || (p && x - a[p - 1] <= a[p] - x)){
--p;
}
cout << sol(p, p, 0) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
22228 KB |
Output is correct |
2 |
Incorrect |
13 ms |
22464 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
22228 KB |
Output is correct |
2 |
Incorrect |
13 ms |
22464 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22244 KB |
Output is correct |
2 |
Correct |
11 ms |
22228 KB |
Output is correct |
3 |
Incorrect |
11 ms |
22144 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
22228 KB |
Output is correct |
2 |
Incorrect |
13 ms |
22464 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |