#include<bits/stdc++.h>
#define MAXN 200007
using namespace std;
const long long inf=1e15;
long long n,t[MAXN],q,s;
long long solve(long long s,int from,int to){
int l=from,r=to;
long long res=0;
while(l!=0 or r!=n+1){
if(l>=1 and s-t[l]<=t[r]-s){
int ll=0,rr=l,mid;
while(ll+1<rr){
mid=(ll+rr)/2;
if(s-t[mid]<2*(s-t[l])){
rr=mid;
}else{
ll=mid;
}
}
res+=s-t[rr];
l=rr-1; s=t[rr];
}else if(r<=n and t[r]-s<=s-t[l]){
int ll=r,rr=n+1,mid;
while(ll+1<rr){
mid=(ll+rr)/2;
if(t[mid]-s<2*(t[r]-s)){
ll=mid;
}else{
rr=mid;
}
}
res+=t[ll]-s;
r=ll+1; s=t[ll];
}else{
exit(0);
}
}
return res;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>t[i];
}
t[n+1]=inf;
t[0]=-inf;
cin>>q;
for(int i=1;i<=q;i++){
cin>>s;
int l=0,r=n+1,mid;
while(l+1<r){
mid=(l+r)/2;
if(t[mid]<=s){
l=mid;
}else{
r=mid;
}
}
cout<<solve(s,l,l+1)<<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 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 |
600 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 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 |
600 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
16 ms |
3932 KB |
Output is correct |
18 |
Correct |
15 ms |
3920 KB |
Output is correct |
19 |
Correct |
15 ms |
3932 KB |
Output is correct |
20 |
Correct |
16 ms |
3880 KB |
Output is correct |
21 |
Correct |
16 ms |
3916 KB |
Output is correct |
22 |
Correct |
16 ms |
3740 KB |
Output is correct |
23 |
Correct |
18 ms |
3932 KB |
Output is correct |
24 |
Correct |
18 ms |
3960 KB |
Output is correct |
25 |
Correct |
17 ms |
3932 KB |
Output is correct |
26 |
Correct |
16 ms |
3932 KB |
Output is correct |
27 |
Correct |
17 ms |
3760 KB |
Output is correct |
28 |
Correct |
17 ms |
3732 KB |
Output is correct |
29 |
Correct |
21 ms |
3676 KB |
Output is correct |
30 |
Correct |
15 ms |
3800 KB |
Output is correct |
31 |
Correct |
15 ms |
3676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
3077 ms |
2004 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 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 |
600 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
16 ms |
3932 KB |
Output is correct |
18 |
Correct |
15 ms |
3920 KB |
Output is correct |
19 |
Correct |
15 ms |
3932 KB |
Output is correct |
20 |
Correct |
16 ms |
3880 KB |
Output is correct |
21 |
Correct |
16 ms |
3916 KB |
Output is correct |
22 |
Correct |
16 ms |
3740 KB |
Output is correct |
23 |
Correct |
18 ms |
3932 KB |
Output is correct |
24 |
Correct |
18 ms |
3960 KB |
Output is correct |
25 |
Correct |
17 ms |
3932 KB |
Output is correct |
26 |
Correct |
16 ms |
3932 KB |
Output is correct |
27 |
Correct |
17 ms |
3760 KB |
Output is correct |
28 |
Correct |
17 ms |
3732 KB |
Output is correct |
29 |
Correct |
21 ms |
3676 KB |
Output is correct |
30 |
Correct |
15 ms |
3800 KB |
Output is correct |
31 |
Correct |
15 ms |
3676 KB |
Output is correct |
32 |
Correct |
0 ms |
348 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 |
3077 ms |
2004 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |