#include <bits/stdc++.h>
using namespace std;
priority_queue<int> lpq;
priority_queue<int, vector<int>, greater<int>> rpq;
int64_t L, R;
void insert(int x) {
int median = (lpq.size() ? lpq.top() : 1000000001);
if (x <= median) {
lpq.push(x);
L += x;
} else {
rpq.push(x);
R += x;
}
if (rpq.size() + 1 < lpq.size()) {
int nxt = lpq.top();
lpq.pop();
rpq.push(nxt);
L -= nxt;
R += nxt;
} else if (lpq.size() < rpq.size()) {
int nxt = rpq.top();
rpq.pop();
lpq.push(nxt);
R -= nxt;
L += nxt;
}
}
int64_t pref[100001];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int k, n;
int64_t same_side = 0;
vector<array<int, 2>> v = {{0, 0}};
cin >> k >> n;
for(int i = 0; i < n; i++) {
char a, b;
int x, y;
cin >> a >> x >> b >> y;
if (a == b) same_side += abs(x - y);
else v.push_back({x, y});
}
sort(v.begin(), v.end(), [](array<int, 2>&a, array<int, 2>&b){
return (a[0] + a[1] < b[0] + b[1]);
});
n = v.size() - 1;
same_side += n;
L = R = 0;
for(int i = 1; i < n + 1; i++) {
insert(v[i][0]);
insert(v[i][1]);
pref[i] = R - L;
}
int64_t ans = pref[n];
if (k == 2) {
while (lpq.size()) lpq.pop();
while (rpq.size()) rpq.pop();
L = R = 0;
for(int i = n; i; i--) {
insert(v[i][0]);
insert(v[i][1]);
ans = min(ans, R - L + pref[i - 1]);
}
}
cout << same_side + ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
30 ms |
3944 KB |
Output is correct |
13 |
Correct |
57 ms |
5608 KB |
Output is correct |
14 |
Correct |
45 ms |
4712 KB |
Output is correct |
15 |
Correct |
34 ms |
3564 KB |
Output is correct |
16 |
Correct |
38 ms |
4968 KB |
Output is correct |
17 |
Correct |
44 ms |
5480 KB |
Output is correct |
18 |
Correct |
47 ms |
5608 KB |
Output is correct |
19 |
Correct |
52 ms |
5480 KB |
Output is correct |
20 |
Correct |
38 ms |
5480 KB |
Output is correct |
21 |
Correct |
48 ms |
5224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
2 ms |
364 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
3 ms |
364 KB |
Output is correct |
21 |
Correct |
2 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
364 KB |
Output is correct |
23 |
Correct |
2 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
25 |
Correct |
43 ms |
3944 KB |
Output is correct |
26 |
Correct |
71 ms |
4584 KB |
Output is correct |
27 |
Correct |
94 ms |
5096 KB |
Output is correct |
28 |
Correct |
96 ms |
5736 KB |
Output is correct |
29 |
Correct |
96 ms |
5480 KB |
Output is correct |
30 |
Correct |
50 ms |
3180 KB |
Output is correct |
31 |
Correct |
50 ms |
4840 KB |
Output is correct |
32 |
Correct |
71 ms |
5480 KB |
Output is correct |
33 |
Correct |
77 ms |
5096 KB |
Output is correct |
34 |
Correct |
82 ms |
5480 KB |
Output is correct |
35 |
Correct |
51 ms |
5480 KB |
Output is correct |
36 |
Correct |
75 ms |
5224 KB |
Output is correct |
37 |
Correct |
35 ms |
3944 KB |
Output is correct |