#include <bits/stdc++.h>
#define int long long
using namespace std;
#define PQ priority_queue
#define PQG(x) PQ<x,vector<x>,greater<x>>
bool cmp(pair<int, int> a, pair<int, int> b) {
return a.first + a.second < b.first + b.second;
}
PQ<int> l;
PQG(int) r;
int ls, rs, arr[100100];
void ins(int x) {
int m = (l.size() ? l.top() : (int)(1e9+7));
if (x <= m) {
l.push(x);
ls += x;
} else {
r.push(x);
rs += x;
}
if (r.size() + 1 < l.size()) {
int x = l.top();
l.pop();
r.push(x);
ls -= x;
rs += x;
} else if (l.size() < r.size()) {
int x = r.top();
r.pop();
l.push(x);
rs -= x;
ls += x;
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int k, n;
int ans = 0;
vector<pair<int, int>> 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) ans += abs(x - y);
else v.push_back({x, y});
}
sort(v.begin(), v.end(), cmp);
ans += n = v.size()-1;
for(int i = 1; i <= n; i++) {
ins(v[i].first);
ins(v[i].second);
arr[i] = rs - ls;
}
int ans2 = arr[n];
if (k == 2) {
l = PQ<int>();
r = PQG(int)();
ls = rs = 0;
for (int i = n+1; --i;) {
ins(v[i].first);
ins(v[i].second);
ans2 = min(ans2, rs - ls + arr[i - 1]);
}
}
cout << ans + ans2;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
368 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
25 ms |
5572 KB |
Output is correct |
13 |
Correct |
49 ms |
5372 KB |
Output is correct |
14 |
Correct |
39 ms |
5764 KB |
Output is correct |
15 |
Correct |
29 ms |
3572 KB |
Output is correct |
16 |
Correct |
31 ms |
5384 KB |
Output is correct |
17 |
Correct |
40 ms |
5584 KB |
Output is correct |
18 |
Correct |
37 ms |
5968 KB |
Output is correct |
19 |
Correct |
45 ms |
5580 KB |
Output is correct |
20 |
Correct |
32 ms |
5564 KB |
Output is correct |
21 |
Correct |
44 ms |
5492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
316 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
31 ms |
5556 KB |
Output is correct |
26 |
Correct |
50 ms |
6092 KB |
Output is correct |
27 |
Correct |
63 ms |
5616 KB |
Output is correct |
28 |
Correct |
70 ms |
6108 KB |
Output is correct |
29 |
Correct |
73 ms |
5700 KB |
Output is correct |
30 |
Correct |
36 ms |
2960 KB |
Output is correct |
31 |
Correct |
38 ms |
5232 KB |
Output is correct |
32 |
Correct |
51 ms |
5568 KB |
Output is correct |
33 |
Correct |
51 ms |
5672 KB |
Output is correct |
34 |
Correct |
54 ms |
5608 KB |
Output is correct |
35 |
Correct |
41 ms |
5584 KB |
Output is correct |
36 |
Correct |
55 ms |
5620 KB |
Output is correct |
37 |
Correct |
27 ms |
5552 KB |
Output is correct |