#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> d(n), li(n, -1), ri(n, -1);
for (int i = 0; i < n; i++) cin >> d[i];
set<pair<int, int> > id;
for (int i = n - 2; i >= 0; i--)
{
auto it = id.upper_bound({ d[i], n });
ri[i + 1] = (it == id.begin() ? n - 1 : prev(it)->second);
pair<int, int> nw = { 2 * d[i] - d[i + 1], i };
while (!id.empty() && id.rbegin()->first >= nw.first) id.erase(*id.rbegin());
id.insert(nw);
}
ri[0] = n - 1;
id.clear();
for (int i = 0; i + 1 < n; i++)
{
auto it = id.upper_bound({ d[i + 1], n });
li[i] = (it == id.end() ? 0 : it->second);
pair<int, int> nw = { 2 * d[i + 1] - d[i], i + 1 };
while (!id.empty() && id.begin()->first <= nw.first) id.erase(*id.begin());
id.insert(nw);
}
li[n - 1] = 0;
int q;
cin >> q;
while (q--)
{
int x;
cin >> x;
int i = lower_bound(d.begin(), d.end(), x) - d.begin();
if (i == n || (i && x - d[i - 1] <= d[i] - x)) i--;
ll ans = abs(d[i] - x);
while (i && i != n - 1)
{
if (i != ri[i]) ans += (ll)(d[ri[i]] - d[i]), i = ri[i];
else ans += (ll)(d[i] - d[li[i]]), i = li[i];
}
ans += d[n - 1] - d[0];
cout << ans << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
476 KB |
Output is correct |
4 |
Correct |
1 ms |
348 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 |
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 |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
476 KB |
Output is correct |
4 |
Correct |
1 ms |
348 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 |
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 |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
37 ms |
2652 KB |
Output is correct |
18 |
Correct |
37 ms |
2824 KB |
Output is correct |
19 |
Correct |
37 ms |
2652 KB |
Output is correct |
20 |
Correct |
38 ms |
2648 KB |
Output is correct |
21 |
Correct |
38 ms |
2652 KB |
Output is correct |
22 |
Correct |
36 ms |
2648 KB |
Output is correct |
23 |
Correct |
37 ms |
2804 KB |
Output is correct |
24 |
Correct |
35 ms |
2648 KB |
Output is correct |
25 |
Correct |
34 ms |
2800 KB |
Output is correct |
26 |
Correct |
37 ms |
2656 KB |
Output is correct |
27 |
Correct |
35 ms |
2652 KB |
Output is correct |
28 |
Correct |
34 ms |
2648 KB |
Output is correct |
29 |
Correct |
39 ms |
2648 KB |
Output is correct |
30 |
Correct |
41 ms |
2652 KB |
Output is correct |
31 |
Correct |
40 ms |
2648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
75 ms |
4064 KB |
Output is correct |
8 |
Correct |
75 ms |
4132 KB |
Output is correct |
9 |
Correct |
75 ms |
4096 KB |
Output is correct |
10 |
Correct |
75 ms |
4256 KB |
Output is correct |
11 |
Correct |
77 ms |
4172 KB |
Output is correct |
12 |
Correct |
74 ms |
4176 KB |
Output is correct |
13 |
Correct |
28 ms |
2244 KB |
Output is correct |
14 |
Correct |
24 ms |
856 KB |
Output is correct |
15 |
Correct |
79 ms |
4588 KB |
Output is correct |
16 |
Correct |
75 ms |
4436 KB |
Output is correct |
17 |
Correct |
75 ms |
4432 KB |
Output is correct |
18 |
Correct |
30 ms |
2324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
476 KB |
Output is correct |
4 |
Correct |
1 ms |
348 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 |
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 |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
37 ms |
2652 KB |
Output is correct |
18 |
Correct |
37 ms |
2824 KB |
Output is correct |
19 |
Correct |
37 ms |
2652 KB |
Output is correct |
20 |
Correct |
38 ms |
2648 KB |
Output is correct |
21 |
Correct |
38 ms |
2652 KB |
Output is correct |
22 |
Correct |
36 ms |
2648 KB |
Output is correct |
23 |
Correct |
37 ms |
2804 KB |
Output is correct |
24 |
Correct |
35 ms |
2648 KB |
Output is correct |
25 |
Correct |
34 ms |
2800 KB |
Output is correct |
26 |
Correct |
37 ms |
2656 KB |
Output is correct |
27 |
Correct |
35 ms |
2652 KB |
Output is correct |
28 |
Correct |
34 ms |
2648 KB |
Output is correct |
29 |
Correct |
39 ms |
2648 KB |
Output is correct |
30 |
Correct |
41 ms |
2652 KB |
Output is correct |
31 |
Correct |
40 ms |
2648 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
344 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 |
75 ms |
4064 KB |
Output is correct |
39 |
Correct |
75 ms |
4132 KB |
Output is correct |
40 |
Correct |
75 ms |
4096 KB |
Output is correct |
41 |
Correct |
75 ms |
4256 KB |
Output is correct |
42 |
Correct |
77 ms |
4172 KB |
Output is correct |
43 |
Correct |
74 ms |
4176 KB |
Output is correct |
44 |
Correct |
28 ms |
2244 KB |
Output is correct |
45 |
Correct |
24 ms |
856 KB |
Output is correct |
46 |
Correct |
79 ms |
4588 KB |
Output is correct |
47 |
Correct |
75 ms |
4436 KB |
Output is correct |
48 |
Correct |
75 ms |
4432 KB |
Output is correct |
49 |
Correct |
30 ms |
2324 KB |
Output is correct |
50 |
Correct |
95 ms |
4944 KB |
Output is correct |
51 |
Correct |
96 ms |
5004 KB |
Output is correct |
52 |
Correct |
90 ms |
4856 KB |
Output is correct |
53 |
Correct |
95 ms |
4844 KB |
Output is correct |
54 |
Correct |
92 ms |
4848 KB |
Output is correct |
55 |
Correct |
92 ms |
4948 KB |
Output is correct |
56 |
Correct |
74 ms |
4692 KB |
Output is correct |
57 |
Correct |
83 ms |
4700 KB |
Output is correct |
58 |
Correct |
79 ms |
4688 KB |
Output is correct |