#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll ltot, rtot;
priority_queue<int> pql;
priority_queue<int, vector<int>, greater<int>> pqr;
void put(ll x) {
ll med = pql.empty() ? LLONG_MAX : pql.top(), tmp;
if (x <= med) {
pql.push(x);
ltot += x;
} else {
pqr.push(x);
rtot += x;
}
if (pqr.size() + 1 < pql.size()) {
tmp = pql.top();
pql.pop();
pqr.push(tmp);
ltot -= tmp;
rtot += tmp;
} else if (pqr.size() > pql.size()) {
tmp = pqr.top();
pqr.pop();
pql.push(tmp);
ltot += tmp;
rtot -= tmp;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int k, n, i;
cin >> k >> n;
char az, bz;
ll ap, bp, ss = 0, best = 0;
vector<ll> pos;
vector<pair<ll, ll>> ppl;
for (i = 0; i < n; i++) {
cin >> az >> ap >> bz >> bp;
if (az == bz) ss += abs(ap - bp);
else {
if (ap > bp) swap(ap, bp);
ppl.push_back({ap, bp});
ss++;
}
}
n = ppl.size();
sort(ppl.begin(), ppl.end(), [](const pair<int, int>& one, const pair<int, int>& two){return one.first + one.second < two.first + two.second;});
ltot = rtot = 0;
vector<ll> pref(n + 1);
for (i = 0; i < n; i++) {
put(ppl[i].first);
put(ppl[i].second);
pref[i + 1] = rtot - ltot;
}
best = pref[n];
if (k == 2) {
while (not pql.empty()) pql.pop();
while (not pqr.empty()) pqr.pop();
ltot = rtot = 0;
for (i = n - 1; i >= 0; i--) {
put(ppl[i].first);
put(ppl[i].second);
best = min(best, rtot - ltot + pref[i]);
}
}
cout << ss + best;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
508 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
504 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
17 ms |
4820 KB |
Output is correct |
13 |
Correct |
36 ms |
6348 KB |
Output is correct |
14 |
Correct |
28 ms |
5216 KB |
Output is correct |
15 |
Correct |
21 ms |
4056 KB |
Output is correct |
16 |
Correct |
18 ms |
5456 KB |
Output is correct |
17 |
Correct |
29 ms |
6092 KB |
Output is correct |
18 |
Correct |
20 ms |
6092 KB |
Output is correct |
19 |
Correct |
35 ms |
6292 KB |
Output is correct |
20 |
Correct |
25 ms |
6188 KB |
Output is correct |
21 |
Correct |
37 ms |
5832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
460 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 |
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 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
468 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 |
344 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 |
604 KB |
Output is correct |
13 |
Correct |
1 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 |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
344 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
344 KB |
Output is correct |
7 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
504 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 |
25 ms |
4680 KB |
Output is correct |
26 |
Correct |
44 ms |
5076 KB |
Output is correct |
27 |
Correct |
60 ms |
6204 KB |
Output is correct |
28 |
Correct |
64 ms |
6604 KB |
Output is correct |
29 |
Correct |
64 ms |
6860 KB |
Output is correct |
30 |
Correct |
32 ms |
3536 KB |
Output is correct |
31 |
Correct |
23 ms |
5436 KB |
Output is correct |
32 |
Correct |
50 ms |
6092 KB |
Output is correct |
33 |
Correct |
34 ms |
6096 KB |
Output is correct |
34 |
Correct |
57 ms |
6092 KB |
Output is correct |
35 |
Correct |
39 ms |
6348 KB |
Output is correct |
36 |
Correct |
54 ms |
5836 KB |
Output is correct |
37 |
Correct |
26 ms |
4816 KB |
Output is correct |