#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int n, q;
int a[maxn];
long long simulate(int pos)
{
long long ans = 0;
int minPos = pos;
int maxPos = pos;
while (true)
{
//cout << "now " << pos << endl;
//for (int i = 0; i < 1e9; ++i);
if (pos == 1)
{
ans += (a[n] - a[1]);
maxPos = n;
}
else if (pos == n)
{
ans += (a[n] - a[1]);
minPos = 1;
}
else
{
//cout << "decide " << pos << " " << minPos << " " << maxPos << endl;
if (a[pos] - a[minPos - 1] <= a[maxPos + 1] - a[pos]) // go left
{
//cout << "LEFT" << endl;
int l = 1;
int r = pos - 1;
while (l <= r)
{
int mid = (l + r) / 2;
if (a[pos] - a[mid] <= a[maxPos + 1] - a[pos])
{
r = mid - 1;
}
else
{
l = mid + 1;
}
}
ans += (a[pos] - a[l]);
pos = l;
}
else // go right
{
//cout << "RIGHT" << endl;
int l = pos + 1;
int r = n;
while (l <= r)
{
int mid = (l + r) / 2;
if (a[mid] - a[pos] < a[pos] - a[minPos - 1])
{
l = mid + 1;
}
else
{
r = mid - 1;
}
}
ans += (a[r] - a[pos]);
pos = r;
}
minPos = min(minPos, pos);
maxPos = max(maxPos, pos);
}
if (minPos == 1 && maxPos == n)
{
break;
}
}
return ans;
}
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
}
cin >> q;
for (int i = 1; i <= q; ++i)
{
int x;
cin >> x;
int nxt = lower_bound(a + 1, a + n + 1, x) - a;
int prv = nxt - 1;
int where = -1;
if (prv <= 0)
{
where = nxt;
}
else if (nxt > n)
{
where = prv;
}
else
{
if (x - a[prv] <= a[nxt] - x)
{
where = prv;
}
else
{
where = nxt;
}
}
int dist1 = abs(a[where] - x);
if (n == 1)
{
cout << dist1 << "\n";
continue;
}
long long ans = dist1 + simulate(where);
cout << ans << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
476 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
480 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
476 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
480 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
17 ms |
3156 KB |
Output is correct |
18 |
Correct |
16 ms |
2944 KB |
Output is correct |
19 |
Correct |
16 ms |
3164 KB |
Output is correct |
20 |
Correct |
16 ms |
3108 KB |
Output is correct |
21 |
Correct |
16 ms |
3136 KB |
Output is correct |
22 |
Correct |
16 ms |
3168 KB |
Output is correct |
23 |
Correct |
23 ms |
3032 KB |
Output is correct |
24 |
Correct |
18 ms |
2940 KB |
Output is correct |
25 |
Correct |
17 ms |
3152 KB |
Output is correct |
26 |
Correct |
16 ms |
3028 KB |
Output is correct |
27 |
Correct |
17 ms |
2928 KB |
Output is correct |
28 |
Correct |
16 ms |
3164 KB |
Output is correct |
29 |
Correct |
17 ms |
3056 KB |
Output is correct |
30 |
Correct |
15 ms |
2908 KB |
Output is correct |
31 |
Correct |
18 ms |
3044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
260 ms |
5124 KB |
Output is correct |
8 |
Correct |
259 ms |
5072 KB |
Output is correct |
9 |
Correct |
249 ms |
4932 KB |
Output is correct |
10 |
Correct |
277 ms |
4936 KB |
Output is correct |
11 |
Correct |
259 ms |
5148 KB |
Output is correct |
12 |
Correct |
270 ms |
5560 KB |
Output is correct |
13 |
Correct |
36 ms |
4296 KB |
Output is correct |
14 |
Correct |
23 ms |
1628 KB |
Output is correct |
15 |
Correct |
230 ms |
6104 KB |
Output is correct |
16 |
Correct |
188 ms |
6016 KB |
Output is correct |
17 |
Correct |
192 ms |
5972 KB |
Output is correct |
18 |
Correct |
32 ms |
4176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
476 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
480 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
17 ms |
3156 KB |
Output is correct |
18 |
Correct |
16 ms |
2944 KB |
Output is correct |
19 |
Correct |
16 ms |
3164 KB |
Output is correct |
20 |
Correct |
16 ms |
3108 KB |
Output is correct |
21 |
Correct |
16 ms |
3136 KB |
Output is correct |
22 |
Correct |
16 ms |
3168 KB |
Output is correct |
23 |
Correct |
23 ms |
3032 KB |
Output is correct |
24 |
Correct |
18 ms |
2940 KB |
Output is correct |
25 |
Correct |
17 ms |
3152 KB |
Output is correct |
26 |
Correct |
16 ms |
3028 KB |
Output is correct |
27 |
Correct |
17 ms |
2928 KB |
Output is correct |
28 |
Correct |
16 ms |
3164 KB |
Output is correct |
29 |
Correct |
17 ms |
3056 KB |
Output is correct |
30 |
Correct |
15 ms |
2908 KB |
Output is correct |
31 |
Correct |
18 ms |
3044 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
260 ms |
5124 KB |
Output is correct |
39 |
Correct |
259 ms |
5072 KB |
Output is correct |
40 |
Correct |
249 ms |
4932 KB |
Output is correct |
41 |
Correct |
277 ms |
4936 KB |
Output is correct |
42 |
Correct |
259 ms |
5148 KB |
Output is correct |
43 |
Correct |
270 ms |
5560 KB |
Output is correct |
44 |
Correct |
36 ms |
4296 KB |
Output is correct |
45 |
Correct |
23 ms |
1628 KB |
Output is correct |
46 |
Correct |
230 ms |
6104 KB |
Output is correct |
47 |
Correct |
188 ms |
6016 KB |
Output is correct |
48 |
Correct |
192 ms |
5972 KB |
Output is correct |
49 |
Correct |
32 ms |
4176 KB |
Output is correct |
50 |
Correct |
314 ms |
7096 KB |
Output is correct |
51 |
Correct |
245 ms |
7252 KB |
Output is correct |
52 |
Correct |
210 ms |
7288 KB |
Output is correct |
53 |
Correct |
347 ms |
7248 KB |
Output is correct |
54 |
Correct |
354 ms |
7292 KB |
Output is correct |
55 |
Correct |
354 ms |
7248 KB |
Output is correct |
56 |
Correct |
55 ms |
6996 KB |
Output is correct |
57 |
Correct |
56 ms |
6992 KB |
Output is correct |
58 |
Correct |
59 ms |
7164 KB |
Output is correct |