#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define size(x) (int)x.size()
template<class S, class T>
bool chmin(S &a, const T &b) {
return a > b && (a = b) == b;
}
template<class S, class T>
bool chmax(S &a, const T &b) {
return a < b && (a = b) == b;
}
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;
const int mod = 998244353;
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
vector<int> x(n);
for (int i = 0; i < n; ++i) {
cin >> x[i];
}
int q;
cin >> q;
while (q--) {
int s, res = 0;
cin >> s;
auto it = lower_bound(all(x), s);
deque<int> l, r;
while (it != x.begin()) {
l.push_front(*--it);
}
it = lower_bound(all(x), s);
while (it != x.end()) {
r.push_back(*it++);
}
while (!l.empty() || !r.empty()) {
if (!l.empty() && !r.empty()) {
if (s - l.back() <= r.front() - s) {
res += s - l.back();
s = l.back();
l.pop_back();
} else {
res += r.front() - s;
s = r.front();
r.pop_front();
}
} else if (!r.empty()) {
res += r.front() - s;
s = r.front();
r.pop_front();
} else {
res += s - l.back();
s = l.back();
l.pop_back();
}
}
cout << res << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
20 ms |
3668 KB |
Output is correct |
18 |
Correct |
16 ms |
3676 KB |
Output is correct |
19 |
Correct |
16 ms |
3672 KB |
Output is correct |
20 |
Correct |
16 ms |
3568 KB |
Output is correct |
21 |
Correct |
16 ms |
3676 KB |
Output is correct |
22 |
Correct |
16 ms |
3676 KB |
Output is correct |
23 |
Correct |
16 ms |
3572 KB |
Output is correct |
24 |
Correct |
16 ms |
3432 KB |
Output is correct |
25 |
Correct |
16 ms |
3568 KB |
Output is correct |
26 |
Correct |
16 ms |
3676 KB |
Output is correct |
27 |
Correct |
18 ms |
3576 KB |
Output is correct |
28 |
Correct |
16 ms |
3576 KB |
Output is correct |
29 |
Correct |
16 ms |
3676 KB |
Output is correct |
30 |
Correct |
16 ms |
3576 KB |
Output is correct |
31 |
Correct |
16 ms |
3568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
Execution timed out |
3072 ms |
5984 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
20 ms |
3668 KB |
Output is correct |
18 |
Correct |
16 ms |
3676 KB |
Output is correct |
19 |
Correct |
16 ms |
3672 KB |
Output is correct |
20 |
Correct |
16 ms |
3568 KB |
Output is correct |
21 |
Correct |
16 ms |
3676 KB |
Output is correct |
22 |
Correct |
16 ms |
3676 KB |
Output is correct |
23 |
Correct |
16 ms |
3572 KB |
Output is correct |
24 |
Correct |
16 ms |
3432 KB |
Output is correct |
25 |
Correct |
16 ms |
3568 KB |
Output is correct |
26 |
Correct |
16 ms |
3676 KB |
Output is correct |
27 |
Correct |
18 ms |
3576 KB |
Output is correct |
28 |
Correct |
16 ms |
3576 KB |
Output is correct |
29 |
Correct |
16 ms |
3676 KB |
Output is correct |
30 |
Correct |
16 ms |
3576 KB |
Output is correct |
31 |
Correct |
16 ms |
3568 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 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 |
Execution timed out |
3072 ms |
5984 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |