#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 (r.empty() || 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(l.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() << endl;
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 |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
93 ms |
11200 KB |
Output is correct |
13 |
Correct |
160 ms |
11184 KB |
Output is correct |
14 |
Correct |
127 ms |
10084 KB |
Output is correct |
15 |
Correct |
79 ms |
6680 KB |
Output is correct |
16 |
Correct |
115 ms |
11268 KB |
Output is correct |
17 |
Correct |
108 ms |
11288 KB |
Output is correct |
18 |
Correct |
83 ms |
11200 KB |
Output is correct |
19 |
Correct |
114 ms |
11248 KB |
Output is correct |
20 |
Correct |
106 ms |
11260 KB |
Output is correct |
21 |
Correct |
112 ms |
11212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
312 KB |
Output is correct |
19 |
Correct |
2 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
480 KB |
Output is correct |
21 |
Correct |
2 ms |
460 KB |
Output is correct |
22 |
Correct |
2 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
532 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
460 KB |
Output is correct |
21 |
Correct |
2 ms |
460 KB |
Output is correct |
22 |
Correct |
2 ms |
452 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
2 ms |
460 KB |
Output is correct |
25 |
Correct |
159 ms |
21344 KB |
Output is correct |
26 |
Correct |
251 ms |
21580 KB |
Output is correct |
27 |
Correct |
252 ms |
22436 KB |
Output is correct |
28 |
Correct |
257 ms |
22892 KB |
Output is correct |
29 |
Correct |
258 ms |
22972 KB |
Output is correct |
30 |
Correct |
117 ms |
12280 KB |
Output is correct |
31 |
Correct |
175 ms |
22332 KB |
Output is correct |
32 |
Correct |
184 ms |
22964 KB |
Output is correct |
33 |
Correct |
137 ms |
22592 KB |
Output is correct |
34 |
Correct |
188 ms |
23000 KB |
Output is correct |
35 |
Correct |
185 ms |
22560 KB |
Output is correct |
36 |
Correct |
188 ms |
22712 KB |
Output is correct |
37 |
Correct |
177 ms |
21400 KB |
Output is correct |