#include <bits/stdc++.h>
using namespace std;
#define int long long
priority_queue<int> lpq;
priority_queue<int, vector<int>, greater<int> > rpq;
int lsum = 0, rsum = 0;
void insrt(int x) {
int med;
if (lpq.size()) med = lpq.top();
else med = 1e9;
if (x <= med) {
lpq.push(x);
lsum += x;
} else {
rpq.push(x);
rsum += x;
}
if (lpq.size() > rpq.size()+1) {
int tmp = lpq.top();
lpq.pop();
rpq.push(tmp);
lsum -= tmp;
rsum += tmp;
}
if (rpq.size() > lpq.size()) {
int tmp = rpq.top();
rpq.pop();
lpq.push(tmp);
rsum -= tmp;
lsum += tmp;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int k, n;
cin >> k >> n;
int m = 0;
vector<tuple<int, int, int>> num;
vector<int> all;
int ans = 0;
for (int i = 0; i < n; i++) {
int a, b;
char c, d;
cin >> c >> a >> d >> b;
if (c == d) {
ans += abs(b-a);
} else {
m++;
ans++;
all.push_back(a);
all.push_back(b);
num.push_back({a+b, a, b});
}
}
sort(all.begin(), all.end());
if (k == 1) {
int x = all[all.size()/2];
for (int i : all) {
ans += abs(i-x);
}
cout << ans << endl;
} else {
sort(num.begin(), num.end());
//find prefixes
vector<int> pref(m);
for (int i = 0; i < m; i++) {
auto [tot, a, b] = num[i];
insrt(a);
insrt(b);
pref[i] = rsum-lsum;
}
//reset arrays
int res = 1e9;
rsum = 0, lsum = 0;
while (lpq.size()) lpq.pop();
while (rpq.size()) rpq.pop();
//find suffixes and answer
for (int i = m-1; i > 0; i--) {
auto [tot, a, b] = num[i];
insrt(a);
insrt(b);
res = min(res, rsum-lsum+pref[i-1]);
}
cout << res+ans << endl;
}
}
//change if to while if wa as may be multiple??
//changes infinities higher if wa
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
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 |
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 |
0 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 |
16 ms |
5284 KB |
Output is correct |
13 |
Correct |
32 ms |
6836 KB |
Output is correct |
14 |
Correct |
23 ms |
5720 KB |
Output is correct |
15 |
Correct |
18 ms |
4188 KB |
Output is correct |
16 |
Correct |
20 ms |
6076 KB |
Output is correct |
17 |
Correct |
22 ms |
6728 KB |
Output is correct |
18 |
Correct |
30 ms |
6364 KB |
Output is correct |
19 |
Correct |
32 ms |
6728 KB |
Output is correct |
20 |
Correct |
21 ms |
6224 KB |
Output is correct |
21 |
Correct |
29 ms |
6556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |