#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std ;
const ll N = 2e5 ;
ll n, q, x[N + 1] ;
signed main()
{
ios_base::sync_with_stdio( 0 ) ;
cin.tie( 0 ) ;
cout.tie( 0 ) ;
cin >> n ;
for(ll i = 1 ; i <= n ; i++)
cin >> x[i] ;
cin >> q ;
if(q == 1)
{
ll ans = 0, pos ;
cin >> pos ;
set<ll> s_neg, s_pos ;
for(ll i = 1 ; i <= n ; i++)
s_pos.insert(x[i]), s_neg.insert(-x[i]) ;
while(s_pos.size())
{
ll l = -1e18, r = 1e18 ;
if(s_pos.lower_bound(pos) != s_pos.end())
r = *s_pos.lower_bound(pos) ;
if(s_neg.lower_bound(-pos) != s_neg.end())
l = -(*s_neg.lower_bound(-pos)) ;
if(pos - l <= r - pos)
{
ans += pos - l ;
pos = l ;
s_neg.erase(-l) ;
s_pos.erase(l) ;
}
else
{
ans += r - pos ;
pos = r ;
s_neg.erase(-r) ;
s_pos.erase(r) ;
}
}
cout << ans ;
return 0 ;
}
return 0 ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
476 KB |
Output is correct |
3 |
Correct |
2 ms |
476 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
472 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
248 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
476 KB |
Output is correct |
3 |
Correct |
2 ms |
476 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
472 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
248 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
121 ms |
21552 KB |
Output is correct |
18 |
Correct |
139 ms |
21552 KB |
Output is correct |
19 |
Correct |
135 ms |
21552 KB |
Output is correct |
20 |
Correct |
132 ms |
21436 KB |
Output is correct |
21 |
Correct |
135 ms |
21552 KB |
Output is correct |
22 |
Correct |
129 ms |
21512 KB |
Output is correct |
23 |
Correct |
149 ms |
21552 KB |
Output is correct |
24 |
Correct |
123 ms |
21464 KB |
Output is correct |
25 |
Correct |
122 ms |
21552 KB |
Output is correct |
26 |
Correct |
151 ms |
21604 KB |
Output is correct |
27 |
Correct |
144 ms |
21464 KB |
Output is correct |
28 |
Correct |
137 ms |
21452 KB |
Output is correct |
29 |
Correct |
125 ms |
21452 KB |
Output is correct |
30 |
Correct |
126 ms |
21516 KB |
Output is correct |
31 |
Correct |
126 ms |
21536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
476 KB |
Output is correct |
3 |
Correct |
2 ms |
476 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
472 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
248 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
121 ms |
21552 KB |
Output is correct |
18 |
Correct |
139 ms |
21552 KB |
Output is correct |
19 |
Correct |
135 ms |
21552 KB |
Output is correct |
20 |
Correct |
132 ms |
21436 KB |
Output is correct |
21 |
Correct |
135 ms |
21552 KB |
Output is correct |
22 |
Correct |
129 ms |
21512 KB |
Output is correct |
23 |
Correct |
149 ms |
21552 KB |
Output is correct |
24 |
Correct |
123 ms |
21464 KB |
Output is correct |
25 |
Correct |
122 ms |
21552 KB |
Output is correct |
26 |
Correct |
151 ms |
21604 KB |
Output is correct |
27 |
Correct |
144 ms |
21464 KB |
Output is correct |
28 |
Correct |
137 ms |
21452 KB |
Output is correct |
29 |
Correct |
125 ms |
21452 KB |
Output is correct |
30 |
Correct |
126 ms |
21516 KB |
Output is correct |
31 |
Correct |
126 ms |
21536 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |