#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
#define f first
#define s second
#define mkp make_pair
#define pii pair<int, int>
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int MAXN = 1e5 + 5;
int n, k, add, a, b;
vector<pii> v;
ordered_set<pii> st;
int pref[MAXN], suff[MAXN];
priority_queue<int, vector<int>> l;
priority_queue<int, vector<int>, greater<int>> r;
void insert(int x){
int med = (l.empty() ? 1e18 : l.top());
if(x <= med){
l.push(x);
a += x;
}
else{
r.push(x);
b += x;
}
if(r.size() + 1 < l.size()){
x = l.top();
l.pop();
r.push(x);
a -= x;
b += x;
}
else if(l.size() < r.size()){
x = r.top();
l.push(x);
r.pop();
a += x;
b -= x;
}
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> k >> n;
if(k == 1){
vector<int> temp;
for(int i = 0; i < n; i++){
char a, b;
int x, y;
cin >> a >> x >> b >> y;
if(a == b) add += abs(x - y);
else{
temp.pb(x);
temp.pb(y);
}
}
n = sz(temp) / 2;
sort(all(temp));
int ans = 0;
for(int i = 0; i < 2 * n; i++){
ans += abs(temp[i] - temp[n]);
}
cout << ans + add + n << '\n';
return 0;
}
for(int i = 0; i < n; i++){
char a, b;
int x, y;
cin >> a >> x >> b >> y;
if(a == b) add += abs(x - y);
else v.pb(mkp(x, y));
}
n = sz(v);
if(n == 0){
cout << add << '\n';
return 0;
}
sort(all(v), [](pii &p1, pii &p2){
return (p1.f + p1.s) < (p2.f + p2.s);
});
a = 0, b = 0;
for(int i = 0; i < n; i++){
insert(v[i].f);
insert(v[i].s);
pref[i] = b - a;
}
while(!l.empty()) l.pop();
while(!r.empty()) r.pop();
a = 0, b = 0;
for(int i = n - 1; i >= 0; i--){
insert(v[i].f);
insert(v[i].s);
suff[i] = b - a;
}
int ans = pref[n - 1];
for(int i = 0; i < n - 1; i++){
ans = min(ans, pref[i] + suff[i + 1]);
}
cout << ans + add + n << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 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 |
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 |
14 ms |
2520 KB |
Output is correct |
13 |
Correct |
32 ms |
2520 KB |
Output is correct |
14 |
Correct |
20 ms |
2520 KB |
Output is correct |
15 |
Correct |
16 ms |
1500 KB |
Output is correct |
16 |
Correct |
16 ms |
2520 KB |
Output is correct |
17 |
Correct |
18 ms |
2540 KB |
Output is correct |
18 |
Correct |
21 ms |
2516 KB |
Output is correct |
19 |
Correct |
24 ms |
2520 KB |
Output is correct |
20 |
Correct |
16 ms |
2520 KB |
Output is correct |
21 |
Correct |
22 ms |
2520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 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 |
600 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
520 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
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 |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 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 |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
26 ms |
7308 KB |
Output is correct |
26 |
Correct |
50 ms |
7364 KB |
Output is correct |
27 |
Correct |
65 ms |
8204 KB |
Output is correct |
28 |
Correct |
64 ms |
8704 KB |
Output is correct |
29 |
Correct |
75 ms |
8392 KB |
Output is correct |
30 |
Correct |
30 ms |
4544 KB |
Output is correct |
31 |
Correct |
29 ms |
7512 KB |
Output is correct |
32 |
Correct |
46 ms |
8644 KB |
Output is correct |
33 |
Correct |
52 ms |
8652 KB |
Output is correct |
34 |
Correct |
50 ms |
8640 KB |
Output is correct |
35 |
Correct |
34 ms |
8132 KB |
Output is correct |
36 |
Correct |
49 ms |
8396 KB |
Output is correct |
37 |
Correct |
27 ms |
7212 KB |
Output is correct |