#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
vector <int> X(n);
for ( auto &u: X ) cin >> u;
int q; cin >> q;
vector <int> S(q);
for ( auto &u: S ) cin >> u;
if ( q == 1 ){
int x = S[0];
const int inf = 1e15;
set <int> st{-inf, inf};
for ( int i = 0; i < n; i++ ){
st.insert(X[i]);
}
int ans = 0;
while ( st.size() > 2 ){
auto it = st.lower_bound(x);
assert(*prev(it) <= x && *it >= x);
if ( abs(*prev(it) - x) <= abs(*it - x) ){
ans += x - *prev(it);
x = *prev(it);
st.erase(prev(it));
} else{
ans += -x + *it;
x = *it;
st.erase(it);
}
}
cout << ans << ln;
}
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
460 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 |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
460 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 |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
64 ms |
13396 KB |
Output is correct |
18 |
Correct |
67 ms |
13164 KB |
Output is correct |
19 |
Correct |
62 ms |
13124 KB |
Output is correct |
20 |
Correct |
62 ms |
13272 KB |
Output is correct |
21 |
Correct |
59 ms |
13140 KB |
Output is correct |
22 |
Correct |
69 ms |
13136 KB |
Output is correct |
23 |
Correct |
59 ms |
13456 KB |
Output is correct |
24 |
Correct |
59 ms |
13136 KB |
Output is correct |
25 |
Correct |
61 ms |
13200 KB |
Output is correct |
26 |
Correct |
61 ms |
13280 KB |
Output is correct |
27 |
Correct |
60 ms |
13140 KB |
Output is correct |
28 |
Correct |
60 ms |
13136 KB |
Output is correct |
29 |
Correct |
59 ms |
13132 KB |
Output is correct |
30 |
Correct |
58 ms |
13132 KB |
Output is correct |
31 |
Correct |
62 ms |
13136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 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 |
460 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 |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
64 ms |
13396 KB |
Output is correct |
18 |
Correct |
67 ms |
13164 KB |
Output is correct |
19 |
Correct |
62 ms |
13124 KB |
Output is correct |
20 |
Correct |
62 ms |
13272 KB |
Output is correct |
21 |
Correct |
59 ms |
13140 KB |
Output is correct |
22 |
Correct |
69 ms |
13136 KB |
Output is correct |
23 |
Correct |
59 ms |
13456 KB |
Output is correct |
24 |
Correct |
59 ms |
13136 KB |
Output is correct |
25 |
Correct |
61 ms |
13200 KB |
Output is correct |
26 |
Correct |
61 ms |
13280 KB |
Output is correct |
27 |
Correct |
60 ms |
13140 KB |
Output is correct |
28 |
Correct |
60 ms |
13136 KB |
Output is correct |
29 |
Correct |
59 ms |
13132 KB |
Output is correct |
30 |
Correct |
58 ms |
13132 KB |
Output is correct |
31 |
Correct |
62 ms |
13136 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |