#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<int,int>pii;
typedef pair<pii,pii>pi2;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int r[200005];
int l[200005];
void solve(){
int n;
cin >> n;
int arr[n+5];
for(int x=1;x<=n;x++){
cin >> arr[x];
}
arr[0]=INT_MIN;
arr[n+1]=1e15;
for(int x=1;x<=n;x++){
l[x]=2*arr[x]-arr[x-1];
r[x]=2*arr[x]-arr[x+1];
}
int q;
cin >> q;
for(int x=0;x<q;x++){
int index;
cin >> index;
int cur=0;
int hold=lower_bound(arr+1,arr+n+2,index)-arr;
if(index-arr[hold-1]<=arr[hold]-index) cur=hold-1;
else cur=hold;
int counter=abs(index-arr[cur]);
bool amos=true;
//show3(arr[cur-1],arr[cur-1],arr[cur],arr[cur],arr[cur+1],arr[cur+1]);
if(arr[cur]-arr[cur-1]<=arr[cur+1]-arr[cur]) amos=false;
int left=cur;
int right=cur;
//show(amos,amos);
//show2(left,left,right,right);
while(left>1&&right<n){
if(amos){
//venture right
int target=arr[left-1];
for(int y=cur;y<=n;y++){
right=y;
if(r[y]<=target) break;
}
counter+=abs(arr[left-1]-arr[right]);
counter+=abs(arr[right]-arr[cur]);
cur=left-1;
left--;
amos=false;
}
else{
//venture left
int target=arr[right+1];
for(int y=cur;y>=1;y--){
left=y;
if(l[y]>target) break;
}
counter+=abs(arr[left]-arr[right+1]);
counter+=abs(arr[left]-arr[cur]);
cur=right+1;
right++;
amos=true;
}
//show2(left,left,right,right);
//show(counter,counter);
}
//show(cur,cur);
if(left!=1){
counter+=abs(arr[cur]-arr[1]);
}
if(right!=n){
counter+=abs(arr[cur]-arr[n]);
}
cout << counter << "\n";
}
}
/*
10
1 2 3 4 5 6 7 8 9 10
1
2
*/
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
//cin >> t;
while(t--){
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
2 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
2 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
19 ms |
6236 KB |
Output is correct |
18 |
Correct |
18 ms |
6200 KB |
Output is correct |
19 |
Correct |
19 ms |
6420 KB |
Output is correct |
20 |
Correct |
18 ms |
6236 KB |
Output is correct |
21 |
Correct |
17 ms |
6228 KB |
Output is correct |
22 |
Correct |
18 ms |
6640 KB |
Output is correct |
23 |
Correct |
18 ms |
6228 KB |
Output is correct |
24 |
Correct |
16 ms |
6400 KB |
Output is correct |
25 |
Correct |
17 ms |
6232 KB |
Output is correct |
26 |
Correct |
17 ms |
6232 KB |
Output is correct |
27 |
Correct |
17 ms |
6232 KB |
Output is correct |
28 |
Correct |
17 ms |
6352 KB |
Output is correct |
29 |
Correct |
17 ms |
6232 KB |
Output is correct |
30 |
Correct |
17 ms |
6364 KB |
Output is correct |
31 |
Correct |
17 ms |
6232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2400 KB |
Output is correct |
2 |
Correct |
1 ms |
2404 KB |
Output is correct |
3 |
Correct |
1 ms |
2520 KB |
Output is correct |
4 |
Correct |
1 ms |
2400 KB |
Output is correct |
5 |
Correct |
1 ms |
2400 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Execution timed out |
3074 ms |
7228 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
2 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
19 ms |
6236 KB |
Output is correct |
18 |
Correct |
18 ms |
6200 KB |
Output is correct |
19 |
Correct |
19 ms |
6420 KB |
Output is correct |
20 |
Correct |
18 ms |
6236 KB |
Output is correct |
21 |
Correct |
17 ms |
6228 KB |
Output is correct |
22 |
Correct |
18 ms |
6640 KB |
Output is correct |
23 |
Correct |
18 ms |
6228 KB |
Output is correct |
24 |
Correct |
16 ms |
6400 KB |
Output is correct |
25 |
Correct |
17 ms |
6232 KB |
Output is correct |
26 |
Correct |
17 ms |
6232 KB |
Output is correct |
27 |
Correct |
17 ms |
6232 KB |
Output is correct |
28 |
Correct |
17 ms |
6352 KB |
Output is correct |
29 |
Correct |
17 ms |
6232 KB |
Output is correct |
30 |
Correct |
17 ms |
6364 KB |
Output is correct |
31 |
Correct |
17 ms |
6232 KB |
Output is correct |
32 |
Correct |
1 ms |
2400 KB |
Output is correct |
33 |
Correct |
1 ms |
2404 KB |
Output is correct |
34 |
Correct |
1 ms |
2520 KB |
Output is correct |
35 |
Correct |
1 ms |
2400 KB |
Output is correct |
36 |
Correct |
1 ms |
2400 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Execution timed out |
3074 ms |
7228 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |