#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--);
}
l.push_front(*it);
it = lower_bound(all(x), s);
if (it != x.end()) {
it++;
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |