#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) + abs(a[p] - x) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22228 KB |
Output is correct |
2 |
Correct |
12 ms |
22484 KB |
Output is correct |
3 |
Correct |
12 ms |
22480 KB |
Output is correct |
4 |
Correct |
12 ms |
22356 KB |
Output is correct |
5 |
Correct |
12 ms |
22484 KB |
Output is correct |
6 |
Correct |
12 ms |
22484 KB |
Output is correct |
7 |
Correct |
12 ms |
22476 KB |
Output is correct |
8 |
Correct |
12 ms |
22228 KB |
Output is correct |
9 |
Correct |
11 ms |
22228 KB |
Output is correct |
10 |
Correct |
12 ms |
22228 KB |
Output is correct |
11 |
Correct |
11 ms |
22228 KB |
Output is correct |
12 |
Correct |
11 ms |
22228 KB |
Output is correct |
13 |
Correct |
14 ms |
22356 KB |
Output is correct |
14 |
Correct |
12 ms |
22524 KB |
Output is correct |
15 |
Correct |
12 ms |
22484 KB |
Output is correct |
16 |
Correct |
11 ms |
22392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22228 KB |
Output is correct |
2 |
Correct |
12 ms |
22484 KB |
Output is correct |
3 |
Correct |
12 ms |
22480 KB |
Output is correct |
4 |
Correct |
12 ms |
22356 KB |
Output is correct |
5 |
Correct |
12 ms |
22484 KB |
Output is correct |
6 |
Correct |
12 ms |
22484 KB |
Output is correct |
7 |
Correct |
12 ms |
22476 KB |
Output is correct |
8 |
Correct |
12 ms |
22228 KB |
Output is correct |
9 |
Correct |
11 ms |
22228 KB |
Output is correct |
10 |
Correct |
12 ms |
22228 KB |
Output is correct |
11 |
Correct |
11 ms |
22228 KB |
Output is correct |
12 |
Correct |
11 ms |
22228 KB |
Output is correct |
13 |
Correct |
14 ms |
22356 KB |
Output is correct |
14 |
Correct |
12 ms |
22524 KB |
Output is correct |
15 |
Correct |
12 ms |
22484 KB |
Output is correct |
16 |
Correct |
11 ms |
22392 KB |
Output is correct |
17 |
Correct |
54 ms |
49472 KB |
Output is correct |
18 |
Correct |
57 ms |
50544 KB |
Output is correct |
19 |
Correct |
65 ms |
55572 KB |
Output is correct |
20 |
Correct |
86 ms |
60536 KB |
Output is correct |
21 |
Correct |
84 ms |
61280 KB |
Output is correct |
22 |
Correct |
51 ms |
46724 KB |
Output is correct |
23 |
Correct |
56 ms |
50232 KB |
Output is correct |
24 |
Correct |
80 ms |
61864 KB |
Output is correct |
25 |
Correct |
60 ms |
51976 KB |
Output is correct |
26 |
Correct |
65 ms |
53368 KB |
Output is correct |
27 |
Correct |
78 ms |
55540 KB |
Output is correct |
28 |
Correct |
69 ms |
55996 KB |
Output is correct |
29 |
Correct |
65 ms |
54816 KB |
Output is correct |
30 |
Correct |
70 ms |
54808 KB |
Output is correct |
31 |
Correct |
65 ms |
54780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22148 KB |
Output is correct |
2 |
Correct |
11 ms |
22156 KB |
Output is correct |
3 |
Correct |
12 ms |
22228 KB |
Output is correct |
4 |
Correct |
11 ms |
22148 KB |
Output is correct |
5 |
Correct |
13 ms |
22228 KB |
Output is correct |
6 |
Correct |
11 ms |
22228 KB |
Output is correct |
7 |
Execution timed out |
3081 ms |
650204 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22228 KB |
Output is correct |
2 |
Correct |
12 ms |
22484 KB |
Output is correct |
3 |
Correct |
12 ms |
22480 KB |
Output is correct |
4 |
Correct |
12 ms |
22356 KB |
Output is correct |
5 |
Correct |
12 ms |
22484 KB |
Output is correct |
6 |
Correct |
12 ms |
22484 KB |
Output is correct |
7 |
Correct |
12 ms |
22476 KB |
Output is correct |
8 |
Correct |
12 ms |
22228 KB |
Output is correct |
9 |
Correct |
11 ms |
22228 KB |
Output is correct |
10 |
Correct |
12 ms |
22228 KB |
Output is correct |
11 |
Correct |
11 ms |
22228 KB |
Output is correct |
12 |
Correct |
11 ms |
22228 KB |
Output is correct |
13 |
Correct |
14 ms |
22356 KB |
Output is correct |
14 |
Correct |
12 ms |
22524 KB |
Output is correct |
15 |
Correct |
12 ms |
22484 KB |
Output is correct |
16 |
Correct |
11 ms |
22392 KB |
Output is correct |
17 |
Correct |
54 ms |
49472 KB |
Output is correct |
18 |
Correct |
57 ms |
50544 KB |
Output is correct |
19 |
Correct |
65 ms |
55572 KB |
Output is correct |
20 |
Correct |
86 ms |
60536 KB |
Output is correct |
21 |
Correct |
84 ms |
61280 KB |
Output is correct |
22 |
Correct |
51 ms |
46724 KB |
Output is correct |
23 |
Correct |
56 ms |
50232 KB |
Output is correct |
24 |
Correct |
80 ms |
61864 KB |
Output is correct |
25 |
Correct |
60 ms |
51976 KB |
Output is correct |
26 |
Correct |
65 ms |
53368 KB |
Output is correct |
27 |
Correct |
78 ms |
55540 KB |
Output is correct |
28 |
Correct |
69 ms |
55996 KB |
Output is correct |
29 |
Correct |
65 ms |
54816 KB |
Output is correct |
30 |
Correct |
70 ms |
54808 KB |
Output is correct |
31 |
Correct |
65 ms |
54780 KB |
Output is correct |
32 |
Correct |
11 ms |
22148 KB |
Output is correct |
33 |
Correct |
11 ms |
22156 KB |
Output is correct |
34 |
Correct |
12 ms |
22228 KB |
Output is correct |
35 |
Correct |
11 ms |
22148 KB |
Output is correct |
36 |
Correct |
13 ms |
22228 KB |
Output is correct |
37 |
Correct |
11 ms |
22228 KB |
Output is correct |
38 |
Execution timed out |
3081 ms |
650204 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |