/*
IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
#define F first
#define S second
#define Mp make_pair
#define pb push_back
#define pf push_front
#define size(x) ((ll)x.size())
#define all(x) (x).begin(),(x).end()
#define kill(x) cout << x << '\n', exit(0);
#define fuck(x) cout << "(" << #x << " , " << x << ")" << endl
#define endl '\n'
const int N = 3e5+23, lg = 18;
ll Mod = 1e9+7; //998244353;
inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
ll ans = 1;
a=MOD(a, mod);
while (b) {
if (b & 1) ans = MOD(ans*a, mod);
b >>= 1;
a = MOD(a*a, mod);
}
return ans;
}
int n, q;
vector<ll> vec;
int main () {
ios_base::sync_with_stdio(false), cin.tie(0);
cin>>n;
for(ll x,i=1; i<=n; i++) {
cin>>x; vec.pb(x);
}
cin>>q;
while(q--) {
ll x; cin>>x;
ll ans = 0;
int r=lower_bound(all(vec), x+1) - vec.begin() + 1;
int l = r-1;
while(true) {
// cout<<l<<' '<<r<<endl;
if(l == 0) {
ans += (vec.back() - x);
break;
}
if(r == n+1) {
ans += (x - vec[0]);
break;
}
if(vec[r-1]-x < x-vec[l-1]) {
r = lower_bound(all(vec), x+(x-vec[l-1])) - vec.begin() + 1;
ans += (vec[r-2]-x);
x = vec[r-2];
} else {
l = lower_bound(all(vec), x-(vec[r-1]-x)) - vec.begin();
ans += (x-vec[l]);
x = vec[l];
}
}
cout<<ans<<endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 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 |
344 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 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 |
344 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
16 ms |
3984 KB |
Output is correct |
18 |
Correct |
16 ms |
3884 KB |
Output is correct |
19 |
Correct |
16 ms |
4056 KB |
Output is correct |
20 |
Correct |
16 ms |
4044 KB |
Output is correct |
21 |
Correct |
17 ms |
3988 KB |
Output is correct |
22 |
Correct |
16 ms |
3972 KB |
Output is correct |
23 |
Correct |
17 ms |
4052 KB |
Output is correct |
24 |
Correct |
16 ms |
4332 KB |
Output is correct |
25 |
Correct |
16 ms |
4056 KB |
Output is correct |
26 |
Correct |
21 ms |
4008 KB |
Output is correct |
27 |
Correct |
16 ms |
4056 KB |
Output is correct |
28 |
Correct |
16 ms |
4052 KB |
Output is correct |
29 |
Correct |
16 ms |
3800 KB |
Output is correct |
30 |
Correct |
16 ms |
4052 KB |
Output is correct |
31 |
Correct |
16 ms |
3804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 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 |
Correct |
341 ms |
5912 KB |
Output is correct |
8 |
Correct |
343 ms |
6028 KB |
Output is correct |
9 |
Correct |
369 ms |
6028 KB |
Output is correct |
10 |
Correct |
342 ms |
6164 KB |
Output is correct |
11 |
Correct |
341 ms |
6044 KB |
Output is correct |
12 |
Correct |
357 ms |
6036 KB |
Output is correct |
13 |
Correct |
31 ms |
4176 KB |
Output is correct |
14 |
Correct |
26 ms |
1452 KB |
Output is correct |
15 |
Correct |
230 ms |
7308 KB |
Output is correct |
16 |
Correct |
229 ms |
7112 KB |
Output is correct |
17 |
Correct |
238 ms |
7044 KB |
Output is correct |
18 |
Correct |
32 ms |
4176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 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 |
344 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
16 ms |
3984 KB |
Output is correct |
18 |
Correct |
16 ms |
3884 KB |
Output is correct |
19 |
Correct |
16 ms |
4056 KB |
Output is correct |
20 |
Correct |
16 ms |
4044 KB |
Output is correct |
21 |
Correct |
17 ms |
3988 KB |
Output is correct |
22 |
Correct |
16 ms |
3972 KB |
Output is correct |
23 |
Correct |
17 ms |
4052 KB |
Output is correct |
24 |
Correct |
16 ms |
4332 KB |
Output is correct |
25 |
Correct |
16 ms |
4056 KB |
Output is correct |
26 |
Correct |
21 ms |
4008 KB |
Output is correct |
27 |
Correct |
16 ms |
4056 KB |
Output is correct |
28 |
Correct |
16 ms |
4052 KB |
Output is correct |
29 |
Correct |
16 ms |
3800 KB |
Output is correct |
30 |
Correct |
16 ms |
4052 KB |
Output is correct |
31 |
Correct |
16 ms |
3804 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 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 |
Correct |
341 ms |
5912 KB |
Output is correct |
39 |
Correct |
343 ms |
6028 KB |
Output is correct |
40 |
Correct |
369 ms |
6028 KB |
Output is correct |
41 |
Correct |
342 ms |
6164 KB |
Output is correct |
42 |
Correct |
341 ms |
6044 KB |
Output is correct |
43 |
Correct |
357 ms |
6036 KB |
Output is correct |
44 |
Correct |
31 ms |
4176 KB |
Output is correct |
45 |
Correct |
26 ms |
1452 KB |
Output is correct |
46 |
Correct |
230 ms |
7308 KB |
Output is correct |
47 |
Correct |
229 ms |
7112 KB |
Output is correct |
48 |
Correct |
238 ms |
7044 KB |
Output is correct |
49 |
Correct |
32 ms |
4176 KB |
Output is correct |
50 |
Correct |
311 ms |
8188 KB |
Output is correct |
51 |
Correct |
267 ms |
8008 KB |
Output is correct |
52 |
Correct |
243 ms |
8200 KB |
Output is correct |
53 |
Correct |
409 ms |
8328 KB |
Output is correct |
54 |
Correct |
410 ms |
8132 KB |
Output is correct |
55 |
Correct |
405 ms |
8076 KB |
Output is correct |
56 |
Correct |
60 ms |
7880 KB |
Output is correct |
57 |
Correct |
59 ms |
7876 KB |
Output is correct |
58 |
Correct |
60 ms |
7868 KB |
Output is correct |