#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], lv[NS], rv[NS];
unordered_map<int, int> dp[NS][2];
int sol(int l, int r, int dir){
if(l == 1 && r == n) return 0;
if(dp[l][dir][r]) return dp[l][dir][r];
int ans = 0;
if(!dir){
int nl = l;
while(nl > 1 && lv[nl] <= a[r + 1]) --nl;
ans += a[l] - a[nl];
if(l == nl){
ans += a[r + 1] - a[nl] + sol(nl, r + 1, 1);
}
else{
ans += sol(nl, r, 0);
}
}
else{
int nr = r;
while(nr < n && rv[nr] > a[l - 1]) ++nr;
ans += a[nr] - a[r];
if(r == nr){
ans += a[nr] - a[l - 1] + sol(l - 1, nr, 0);
}
else{
ans += sol(l, nr, 1);
}
}
return (dp[l][dir][r] = ans);
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i){
cin >> a[i];
}
a[0] = -(int)1e18, a[n + 1] = (int)1e18;
for(int i = 1; i <= n; ++i){
lv[i] = a[i] * 2 - a[i - 1];
rv[i] = 2 * a[i] - a[i + 1];
}
cin >> q;
while(q--){
int x;
cin >> x;
int p = lower_bound(a + 1, a + n + 1, x) - a;
if(p == n + 1 || (p > 1 && x - a[p - 1] <= a[p] - x)){
--p;
}
cout << sol(p, p, 0) + abs(a[p] - x) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
22228 KB |
Output is correct |
2 |
Correct |
12 ms |
22228 KB |
Output is correct |
3 |
Correct |
12 ms |
22304 KB |
Output is correct |
4 |
Correct |
12 ms |
22304 KB |
Output is correct |
5 |
Correct |
12 ms |
22308 KB |
Output is correct |
6 |
Correct |
13 ms |
22228 KB |
Output is correct |
7 |
Correct |
12 ms |
22228 KB |
Output is correct |
8 |
Correct |
12 ms |
22216 KB |
Output is correct |
9 |
Correct |
12 ms |
22204 KB |
Output is correct |
10 |
Correct |
14 ms |
22228 KB |
Output is correct |
11 |
Correct |
13 ms |
22228 KB |
Output is correct |
12 |
Correct |
12 ms |
22192 KB |
Output is correct |
13 |
Correct |
13 ms |
22236 KB |
Output is correct |
14 |
Correct |
12 ms |
22312 KB |
Output is correct |
15 |
Correct |
12 ms |
22228 KB |
Output is correct |
16 |
Correct |
12 ms |
22288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
22228 KB |
Output is correct |
2 |
Correct |
12 ms |
22228 KB |
Output is correct |
3 |
Correct |
12 ms |
22304 KB |
Output is correct |
4 |
Correct |
12 ms |
22304 KB |
Output is correct |
5 |
Correct |
12 ms |
22308 KB |
Output is correct |
6 |
Correct |
13 ms |
22228 KB |
Output is correct |
7 |
Correct |
12 ms |
22228 KB |
Output is correct |
8 |
Correct |
12 ms |
22216 KB |
Output is correct |
9 |
Correct |
12 ms |
22204 KB |
Output is correct |
10 |
Correct |
14 ms |
22228 KB |
Output is correct |
11 |
Correct |
13 ms |
22228 KB |
Output is correct |
12 |
Correct |
12 ms |
22192 KB |
Output is correct |
13 |
Correct |
13 ms |
22236 KB |
Output is correct |
14 |
Correct |
12 ms |
22312 KB |
Output is correct |
15 |
Correct |
12 ms |
22228 KB |
Output is correct |
16 |
Correct |
12 ms |
22288 KB |
Output is correct |
17 |
Correct |
28 ms |
26880 KB |
Output is correct |
18 |
Correct |
28 ms |
26836 KB |
Output is correct |
19 |
Correct |
36 ms |
26912 KB |
Output is correct |
20 |
Correct |
29 ms |
26860 KB |
Output is correct |
21 |
Correct |
30 ms |
26872 KB |
Output is correct |
22 |
Correct |
28 ms |
26880 KB |
Output is correct |
23 |
Correct |
28 ms |
26900 KB |
Output is correct |
24 |
Correct |
28 ms |
26828 KB |
Output is correct |
25 |
Correct |
29 ms |
26916 KB |
Output is correct |
26 |
Correct |
29 ms |
26852 KB |
Output is correct |
27 |
Correct |
29 ms |
26872 KB |
Output is correct |
28 |
Correct |
28 ms |
26796 KB |
Output is correct |
29 |
Correct |
28 ms |
26924 KB |
Output is correct |
30 |
Correct |
28 ms |
26856 KB |
Output is correct |
31 |
Correct |
28 ms |
26868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
22228 KB |
Output is correct |
2 |
Correct |
12 ms |
22260 KB |
Output is correct |
3 |
Correct |
12 ms |
22228 KB |
Output is correct |
4 |
Correct |
12 ms |
22228 KB |
Output is correct |
5 |
Correct |
12 ms |
22248 KB |
Output is correct |
6 |
Correct |
14 ms |
22156 KB |
Output is correct |
7 |
Execution timed out |
3075 ms |
41156 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
22228 KB |
Output is correct |
2 |
Correct |
12 ms |
22228 KB |
Output is correct |
3 |
Correct |
12 ms |
22304 KB |
Output is correct |
4 |
Correct |
12 ms |
22304 KB |
Output is correct |
5 |
Correct |
12 ms |
22308 KB |
Output is correct |
6 |
Correct |
13 ms |
22228 KB |
Output is correct |
7 |
Correct |
12 ms |
22228 KB |
Output is correct |
8 |
Correct |
12 ms |
22216 KB |
Output is correct |
9 |
Correct |
12 ms |
22204 KB |
Output is correct |
10 |
Correct |
14 ms |
22228 KB |
Output is correct |
11 |
Correct |
13 ms |
22228 KB |
Output is correct |
12 |
Correct |
12 ms |
22192 KB |
Output is correct |
13 |
Correct |
13 ms |
22236 KB |
Output is correct |
14 |
Correct |
12 ms |
22312 KB |
Output is correct |
15 |
Correct |
12 ms |
22228 KB |
Output is correct |
16 |
Correct |
12 ms |
22288 KB |
Output is correct |
17 |
Correct |
28 ms |
26880 KB |
Output is correct |
18 |
Correct |
28 ms |
26836 KB |
Output is correct |
19 |
Correct |
36 ms |
26912 KB |
Output is correct |
20 |
Correct |
29 ms |
26860 KB |
Output is correct |
21 |
Correct |
30 ms |
26872 KB |
Output is correct |
22 |
Correct |
28 ms |
26880 KB |
Output is correct |
23 |
Correct |
28 ms |
26900 KB |
Output is correct |
24 |
Correct |
28 ms |
26828 KB |
Output is correct |
25 |
Correct |
29 ms |
26916 KB |
Output is correct |
26 |
Correct |
29 ms |
26852 KB |
Output is correct |
27 |
Correct |
29 ms |
26872 KB |
Output is correct |
28 |
Correct |
28 ms |
26796 KB |
Output is correct |
29 |
Correct |
28 ms |
26924 KB |
Output is correct |
30 |
Correct |
28 ms |
26856 KB |
Output is correct |
31 |
Correct |
28 ms |
26868 KB |
Output is correct |
32 |
Correct |
12 ms |
22228 KB |
Output is correct |
33 |
Correct |
12 ms |
22260 KB |
Output is correct |
34 |
Correct |
12 ms |
22228 KB |
Output is correct |
35 |
Correct |
12 ms |
22228 KB |
Output is correct |
36 |
Correct |
12 ms |
22248 KB |
Output is correct |
37 |
Correct |
14 ms |
22156 KB |
Output is correct |
38 |
Execution timed out |
3075 ms |
41156 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |