#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,a[1000000],q,st;
void solve(){
int ans=0;
auto* it=lower_bound(a+1,a+n+1,st);
int pos=it-a;
if(abs(st-a[pos-1])<abs(a[pos]-st)){
pos--;
}
ans+=abs(st-a[pos]);
int x=pos-1;
int y=pos+1;
//cout<<pos<<endl;
while(1<=x || y<=n){
if(abs(a[x]-a[pos])<=abs(a[y]-a[pos])){
//cout<<"! goleft"<<endl;
int l=1;
int r=x;
while(l<=r){
int mid=(l+r)/2;
if(abs(a[pos]-a[mid])<=abs(a[y]-a[pos])){
r=mid-1;
}
else{
l=mid+1;
}
}
r++;
if(abs(a[r]-a[pos])<=abs(a[pos]-a[y])){
ans+=abs(a[r]-a[pos]);
x=r-1;
pos=r;
}
}
else{
//cout<<"! goright"<<endl;
int l=y;
int r=n;
while(l<=r){
int mid=(l+r)/2;
if(abs(a[mid]-a[pos])<abs(a[x]-a[pos])){
l=mid+1;
}
else{
r=mid-1;
}
}
l--;
if(abs(a[l]-a[pos])<abs(a[pos]-a[x])){
ans+=abs(a[pos]-a[l]);
pos=l;
y=l+1;
}
}
//cout<<x<<' '<<y<<' '<<pos<<endl;
}
cout<<ans<<endl;
}
signed main(){
cin>>n;
for(int i=1;n>=i;i++){
cin>>a[i];
}
a[0]=a[n+1]=1000000000000;
cin>>q;
while(q--){
cin>>st;
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Incorrect |
573 ms |
5736 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |