#include <iostream>
#include <vector>
#include <array>
#include <cstdlib>
#include <algorithm>
#include <map>
#define ll long long
using namespace std;
char C, D;
multimap <ll, ll> mp, mp2;
vector <ll> X;
vector <array<ll, 2> > V;
ll k, n, l, r, f, mn = 1e18, t, sl, sr, tl, tr;
int main() {
cin >> k >> n;
if (k == 1) {
for (int i=0; i<n; ++i) {
cin >> C >> l >> D >> r;
if (l > r) swap(l, r);
if (C == D) f += r-l;
else {
X.push_back(l);
X.push_back(r);
}
}
sort(X.begin(), X.end());
for (auto u : X) {
f += abs(X[X.size()/2]-u);
}
cout << f+X.size()/2 << '\n';
return 0;
}
for (int i=0; i<n; ++i) {
cin >> C >> l >> D >> r;
if (l > r) swap(l, r);
if (C == D) f += r-l;
else V.push_back({l, r});
}
sort(V.begin(), V.end(), [](auto a, auto b) {
return a[0]+a[1] < b[0]+b[1];
});
if (V.size() == 1) f += V[0][1]-V[0][0]+1;
if (V.size() <= 1) {
cout << f << '\n';
return 0;
}
for (int i=0; i<V.size(); ++i) {
mp2.insert({V[i][0], i});
mp2.insert({V[i][1], i});
t += V[i][0]+V[i][1];
}
auto md = mp.begin();
auto md2 = mp2.begin();
for (int i=0; i<V.size()-1; ++i) {
sl += md2->first;
md2 = next(md2);
}
sl += md2->first;
sr = t-sl;
for (int i=0; i<V.size()-1; ++i) {
mp.insert({V[i][0], i});
mp.insert({V[i][1], i});
if (i) {
if (V[i][1] < md->first) {
tl -= md->first;
tr += md->first;
md = prev(md);
tl += V[i][0]+V[i][1];
}
else if (V[i][0] >= md->first) {
md = next(md);
tl += md->first;
tr -= md->first;
tr += V[i][0]+V[i][1];
}
else tl += V[i][0], tr += V[i][1];
}
else {
md = mp.begin();
tl += V[i][0], tr += V[i][1];
}
if (V[i][0] == V[i][1] && V[i][0] == md2->first) {
auto z = prev(md2);
if (z->first != md2->first) {
sl -= md2->first;
sr += md2->first;
md2 = prev(md2);
sr -= V[i][0]+V[i][1];
}
else {
md2 = next(md2);
sl += md2->first;
sr -= md2->first;
sl -= V[i][0]+V[i][1];
}
}
else if (V[i][1] <= md2->first) {
md2 = next(md2);
sl += md2->first;
sr -= md2->first;
sl -= V[i][0]+V[i][1];
}
else if (md2->first <= V[i][0]) {
sl -= md2->first;
sr += md2->first;
md2 = prev(md2);
sr -= V[i][0]+V[i][1];
}
else sl -= V[i][0], sr -= V[i][1];
mp2.erase(mp2.lower_bound(V[i][0]));
mp2.erase(mp2.lower_bound(V[i][1]));
mn = min(mn, ((md->first+next(md)->first)/2)*((ll)mp.size()/2)-tl+tr-((md->first+next(md)->first)/2)*((ll)mp.size()/2)+
((md2->first+next(md2)->first)/2)*((ll)mp2.size()/2)-sl+sr-((md2->first+next(md2)->first)/2)*((ll)mp2.size()/2));
}
cout << mn+V.size()+f << '\n';
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:50:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for (int i=0; i<V.size(); ++i) {
| ~^~~~~~~~~
bridge.cpp:57:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for (int i=0; i<V.size()-1; ++i) {
| ~^~~~~~~~~~~
bridge.cpp:63:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int i=0; i<V.size()-1; ++i) {
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
444 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
36 ms |
3228 KB |
Output is correct |
13 |
Correct |
81 ms |
4252 KB |
Output is correct |
14 |
Correct |
51 ms |
3588 KB |
Output is correct |
15 |
Correct |
46 ms |
2624 KB |
Output is correct |
16 |
Correct |
55 ms |
3636 KB |
Output is correct |
17 |
Correct |
68 ms |
4244 KB |
Output is correct |
18 |
Correct |
65 ms |
3980 KB |
Output is correct |
19 |
Correct |
77 ms |
4204 KB |
Output is correct |
20 |
Correct |
61 ms |
3772 KB |
Output is correct |
21 |
Correct |
81 ms |
4008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
300 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
0 ms |
300 KB |
Output is correct |
12 |
Correct |
1 ms |
292 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
300 KB |
Output is correct |
2 |
Correct |
0 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
440 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
440 KB |
Output is correct |
25 |
Correct |
134 ms |
15128 KB |
Output is correct |
26 |
Correct |
172 ms |
15328 KB |
Output is correct |
27 |
Correct |
233 ms |
16060 KB |
Output is correct |
28 |
Correct |
286 ms |
16704 KB |
Output is correct |
29 |
Correct |
267 ms |
16604 KB |
Output is correct |
30 |
Correct |
105 ms |
8896 KB |
Output is correct |
31 |
Correct |
152 ms |
15932 KB |
Output is correct |
32 |
Correct |
152 ms |
16712 KB |
Output is correct |
33 |
Correct |
124 ms |
16220 KB |
Output is correct |
34 |
Correct |
149 ms |
16700 KB |
Output is correct |
35 |
Correct |
172 ms |
16340 KB |
Output is correct |
36 |
Correct |
142 ms |
16456 KB |
Output is correct |
37 |
Correct |
119 ms |
15164 KB |
Output is correct |