#include "bits/stdc++.h"
using namespace std;
using ll = long long;
struct median {
multiset<int> l, r;
int total = 0;
ll leftC = 0, rightC = 0;
void insert(int c) {
total++;
if (c < *r.begin()) {
l.insert(c);
leftC += c;
} else {
r.insert(c);
rightC += c;
}
if (l.size() < r.size()) {
int cur = *r.begin();
r.erase(r.find(cur));
rightC -= cur;
leftC += cur;
l.insert(cur);
} else if (r.size() + 1 < l.size()) {
int cur = *l.rbegin();
l.erase(r.find(cur));
leftC -= cur;
rightC += cur;
r.insert(cur);
}
}
ll query() {
return rightC - leftC;
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("", "r", stdin);
// freopen("", "w", stdout);
int n, k; cin >> k >> n;
ll over = 0;
vector<pair<int, int>> v;
for (int i = 0; i < n; i++) {
char a, b; int c, d; cin >> a >> c >> b >> d;
if (a == b) {
over += abs(c - d);
} else {
v.push_back({c, d});
}
}
sort(v.begin(), v.end(), [](pair<int, int> a, pair<int, int> b) {
return (a.first + a.second) / 2 < (b.first + b.second) / 2;
});
median m1;
vector<ll> psum(n+1);
for (int i = 0; i < v.size(); i++) {
m1.insert(v[i].first);
m1.insert(v[i].second);
psum[i] = m1.query();
}
ll ans = m1.query();
median m2;
if (k == 1) cout << over + ans + v.size();
else {
for (int i = v.size()-1; i > 0; i--) {
m2.insert(v[i].first);
m2.insert(v[i].second);
ans = min(ans, psum[i-1] + m2.query());
}
cout << ans + over + v.size() << endl;
return 0;
}
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:67:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Runtime error |
1 ms |
440 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |