#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
#define int long long
using namespace std;
signed main(){
AquA;
int n;
cin >> n;
vector<int> v(n+2);
for(int i=1;i<=n;i++){
cin >> v[i];
}
const int inf=1e18;
v[0]=-inf;
v[n+1]=inf;
int q;
cin >> q;
while(q--){
int c;
cin >> c;
int p=lower_bound(v.begin(),v.end(),c)-v.begin();
int l,r;
int ans=0;
if(c-v[p-1]<=v[p]-c){
p--;
}
l=r=p;
ans+=abs(c-v[p]);
while(l!=1 || r!=n){
if(abs(v[p]-v[l-1])<=abs(v[r+1]-v[p])){
int a=1,b=l-1;
while(b>a){
int m=(a+b+1)/2;
if(abs(v[m]-v[r+1])>=abs(v[m]-v[m-1])){
b=m-1;
}
else{
a=m;
}
}
ans+=abs(v[p]-v[a]);
assert(abs(v[a]-v[a-1])>=abs(v[a]-v[r+1]));
l=p=a;
}
else{
int a=r+1,b=n;
while(b>a){
int m=(a+b)/2;
if(abs(v[m]-v[l-1])>abs(v[m]-v[m+1])){
a=m+1;
}
else{
b=m;
}
}
ans+=abs(v[p]-v[a]);
r=p=a;
}
}
cout << ans << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
232 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
124 ms |
3256 KB |
Output is correct |
8 |
Correct |
114 ms |
3172 KB |
Output is correct |
9 |
Correct |
113 ms |
3160 KB |
Output is correct |
10 |
Correct |
115 ms |
3268 KB |
Output is correct |
11 |
Correct |
118 ms |
3216 KB |
Output is correct |
12 |
Correct |
116 ms |
3276 KB |
Output is correct |
13 |
Correct |
32 ms |
2164 KB |
Output is correct |
14 |
Correct |
23 ms |
860 KB |
Output is correct |
15 |
Incorrect |
97 ms |
3564 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |