#include <bits/stdc++.h>
using namespace std;
vector<long long> get(vector<pair<int, int>> v) {
int n = v.size();
multiset<int> l, r;
vector<long long> p(n);
long long suml = 0, sumr = 0;
auto insert = [&](int x) {
r.insert(x);
sumr += x;
if (!l.empty() && *prev(l.end()) > *r.begin()) {
sumr += *prev(l.end());
suml -= *prev(l.end());
r.insert(*prev(l.end()));
l.erase(prev(l.end()));
sumr -= *r.begin();
suml += *r.begin();
l.insert(*r.begin());
r.erase(r.begin());
}
if (l.size() < r.size()) {
sumr -= *r.begin();
suml += *r.begin();
l.insert(*r.begin());
r.erase(r.begin());
}
};
for (int i = 0; i < n; i++) {
insert(v[i].first);
insert(v[i].second);
int median = *prev(l.end());
p[i] = 1LL * l.size() * median - suml + sumr - 1LL * r.size() * median;
}
return p;
}
void testCase() {
int k, n;
cin >> k >> n;
vector<pair<int, int>> v;
long long ans = 0;
for (int i = 0; i < n; i++) {
int x1, x2;
char t1, t2;
cin >> t1 >> x1 >> t2 >> x2;
if (t1 == t2) ans += abs(x1 - x2);
else ans++, v.push_back({min(x1, x2), max(x1, x2)});
}
sort(v.begin(), v.end(), [&](pair<int, int> i, pair<int, int> j) { return i.first + i.second < j.first + j.second; });
if (v.empty()) {
cout << ans << "\n";
return;
}
auto p = get(v);
reverse(v.begin(), v.end());
auto s = get(v);
reverse(s.begin(), s.end());
if (k == 1) {
cout << ans + p.back() << "\n";
return;
}
long long mn = min(p.back(), s[0]);
for (int i = 1; i < v.size(); i++) {
mn = min(mn, p[i - 1] + s[i]);
}
cout << ans + mn << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
return 0;
}
Compilation message
bridge.cpp: In function 'void testCase()':
bridge.cpp:62:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int i = 1; i < v.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
584 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
496 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
130 ms |
13724 KB |
Output is correct |
13 |
Correct |
180 ms |
15160 KB |
Output is correct |
14 |
Correct |
210 ms |
12876 KB |
Output is correct |
15 |
Correct |
96 ms |
8984 KB |
Output is correct |
16 |
Correct |
98 ms |
14432 KB |
Output is correct |
17 |
Correct |
150 ms |
15176 KB |
Output is correct |
18 |
Correct |
117 ms |
14784 KB |
Output is correct |
19 |
Correct |
153 ms |
15128 KB |
Output is correct |
20 |
Correct |
121 ms |
14704 KB |
Output is correct |
21 |
Correct |
150 ms |
14740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
452 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 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 |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 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 |
2 ms |
528 KB |
Output is correct |
18 |
Correct |
1 ms |
456 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
404 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
452 KB |
Output is correct |
2 |
Correct |
1 ms |
356 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 |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
456 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
133 ms |
13736 KB |
Output is correct |
26 |
Correct |
172 ms |
13700 KB |
Output is correct |
27 |
Correct |
206 ms |
14576 KB |
Output is correct |
28 |
Correct |
193 ms |
15196 KB |
Output is correct |
29 |
Correct |
198 ms |
15272 KB |
Output is correct |
30 |
Correct |
86 ms |
7988 KB |
Output is correct |
31 |
Correct |
94 ms |
14204 KB |
Output is correct |
32 |
Correct |
160 ms |
14716 KB |
Output is correct |
33 |
Correct |
127 ms |
14512 KB |
Output is correct |
34 |
Correct |
159 ms |
14792 KB |
Output is correct |
35 |
Correct |
132 ms |
14376 KB |
Output is correct |
36 |
Correct |
148 ms |
14756 KB |
Output is correct |
37 |
Correct |
114 ms |
13612 KB |
Output is correct |