#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
#define pb push_back
#define pii pair <int,int>
#define ll long long
#define left h*2,l,(l + r)/2
#define right h*2+1,(l + r)/2 + 1,r
using namespace std;
const int N = 3e5 + 5,mod = 1e9 + 7;
int a[N];
int main() {
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n;
cin>>n;
for (int i = 1; i <= n; i++){
cin >> a[i];
}
int q;
cin>>q;
while (q--){
int s;
cin>>s;
int le = 1,ri = n,R = n + 1;
while (le <= ri){
int mid = (le + ri) >> 1;
if (a[mid] > s) {
R = mid;
ri = mid - 1;
}else{
le = mid + 1;
}
}
le = 1; ri = n; int L = 0;
while (le <= ri){
int mid = (le + ri) >> 1;
if (a[mid] < s){
L = mid;
le = mid + 1;
}else{
ri = mid - 1;
}
}
int ans=0;
if (L == 0){
ans += (a[n] - s);
cout<<ans<<endl;
continue;
}else if (R == n + 1){
ans += (s - a[1]);
cout<<ans<<endl;
continue;
}
int dir = 1;
//dir == 1 -> right
//dir == 0 <- left
if (a[R] - s < s - a[L]){
dir = 1;
ans += (a[R] - s);
s = a[R];
}else{
dir = 0;
ans += (s - a[L]);
s = a[L];
}
while (L != 0 && R != n + 1){
if (dir == 1){
// vedzebt R -ze meti an tol umcires r-s romlistvisac
//2*x[r] - x[r + 1] <= x[L]
int r = n;
for (int i = R; i < n; i++){
if (2*a[i] - a[i + 1] <= a[L]) {r = i; break;}
}
ans += a[r] - s;
ans += a[r] - a[L];
s = a[L];
R = r + 1;
}else{
//vedzebt L-ze naklebi an tol udides l-s romlistvisac
//2*x[l] - x[l - 1] > x[R]
int l = 1;
for (int i = L; i > 1; i--){
if (2*a[i] - a[i - 1] > a[R]) {l = i; break;}
}
ans += s - a[l];
ans += a[R] - a[l];
s = a[R];
L = l - 1;
}
dir ^= 1;
}
if (L == 0){
ans += (a[n] - s);
}else if (R == n + 1){
ans += (s - a[1]);
}
cout<<ans<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
21 ms |
3008 KB |
Output is correct |
18 |
Correct |
14 ms |
2972 KB |
Output is correct |
19 |
Correct |
14 ms |
3164 KB |
Output is correct |
20 |
Correct |
14 ms |
3156 KB |
Output is correct |
21 |
Correct |
13 ms |
3156 KB |
Output is correct |
22 |
Correct |
14 ms |
2908 KB |
Output is correct |
23 |
Correct |
14 ms |
3164 KB |
Output is correct |
24 |
Correct |
13 ms |
2908 KB |
Output is correct |
25 |
Correct |
14 ms |
3160 KB |
Output is correct |
26 |
Correct |
14 ms |
3160 KB |
Output is correct |
27 |
Correct |
14 ms |
3164 KB |
Output is correct |
28 |
Correct |
14 ms |
3164 KB |
Output is correct |
29 |
Correct |
16 ms |
2896 KB |
Output is correct |
30 |
Correct |
13 ms |
2904 KB |
Output is correct |
31 |
Correct |
17 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
456 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Execution timed out |
3087 ms |
3616 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
21 ms |
3008 KB |
Output is correct |
18 |
Correct |
14 ms |
2972 KB |
Output is correct |
19 |
Correct |
14 ms |
3164 KB |
Output is correct |
20 |
Correct |
14 ms |
3156 KB |
Output is correct |
21 |
Correct |
13 ms |
3156 KB |
Output is correct |
22 |
Correct |
14 ms |
2908 KB |
Output is correct |
23 |
Correct |
14 ms |
3164 KB |
Output is correct |
24 |
Correct |
13 ms |
2908 KB |
Output is correct |
25 |
Correct |
14 ms |
3160 KB |
Output is correct |
26 |
Correct |
14 ms |
3160 KB |
Output is correct |
27 |
Correct |
14 ms |
3164 KB |
Output is correct |
28 |
Correct |
14 ms |
3164 KB |
Output is correct |
29 |
Correct |
16 ms |
2896 KB |
Output is correct |
30 |
Correct |
13 ms |
2904 KB |
Output is correct |
31 |
Correct |
17 ms |
2908 KB |
Output is correct |
32 |
Correct |
0 ms |
456 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Execution timed out |
3087 ms |
3616 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |