#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long int ll;
int main() {
int k, n;
cin >> k >> n;
if (k != 1) {
vector<pair<int, int> > a;
ll sum = 0;
for (int i = 0; i < n; i++) {
char sa, sb;
int ia, ib;
cin >> sa >> ia >> sb >> ib;
if (sa == sb) {
sum += abs(ia-ib);
continue;
}
a.push_back(make_pair(ia, ib));
sum += 1;
}
if (a.size() == 1) {
cout << sum + abs(a[0].first - a[0].second) << "\n";
}
sort(a.begin(), a.end(), [](auto a, auto b){
return a.first + a.second < b.first + a.second;
});
n = a.size();
vector<ll> lh_s(n+1);
vector<ll> hh_s(n+1);
// for (auto b : a) {
// cout << b.first << " " << b.second << "\n";
// }
for (int q = 0; q < 2; q++) {
// cout << "== " << q << "\n";
if (q == 1) {
for (int i = 0; i < a.size()/2; i++) {
auto tmp = a[i];
a[i] = a[a.size()-i-1];
a[a.size()-i-1] = tmp;
}
}
ll ls = 0, hs = 0;
priority_queue<int> lh;
priority_queue<int, vector<int>, greater<int> > hh;
bool fst = true;
int ptr = q == 0 ? 0 : a.size();
for (auto b : a) {
// cout << "=" << ls << " " << hs << "\n";
if (q == 0) lh_s[ptr++] = hs - ls; else hh_s[ptr--] = hs - ls;
if (fst) {
lh.push(min(b.first, b.second));
hh.push(max(b.first, b.second));
ls = min(b.first, b.second);
hs = max(b.first, b.second);
fst = false;
continue;
}
if (b.first > lh.top()) {
hh.push(b.first);
hs += b.first;
} else {
lh.push(b.first);
ls += b.first;
}
if (b.second > lh.top()) {
hh.push(b.second);
hs += b.second;
} else {
lh.push(b.second);
ls += b.second;
}
if (lh.size() > hh.size()) {
int tmp = lh.top();
ls -= tmp;
hs += tmp;
lh.pop();
hh.push(tmp);
} else if (lh.size() < hh.size()) {
int tmp = hh.top();
hs -= tmp;
ls += tmp;
hh.pop();
lh.push(tmp);
}
// cout << b.first << " " << b.second << "\n";
}
if (q == 0) lh_s[ptr] = hs - ls; else hh_s[ptr] = hs - ls;
}
// for (auto v : lh_s) {
// cout << "l" << v << "\n";
// }
// for (auto v : hh_s) {
// cout << "h" << v << "\n";
// }
ll bs = 0x7FFFFFFFFFFFFFFFLL;
for (int i = 0; i < n+1; i++) {
bs = min(bs, lh_s[i] + hh_s[i]);
}
sum += bs;
cout << sum << "\n";
return 0;
}
vector<int> a;
ll sum = 0;
for (int i = 0; i < n; i++) {
char sa, sb;
int ia, ib;
cin >> sa >> ia >> sb >> ib;
if (sa == sb) {
sum += abs(ia-ib);
continue;
}
a.push_back(ia);
a.push_back(ib);
sum += 1;
}
sort(a.begin(), a.end());
int p = a[a.size() / 2];
for (auto v : a) {
sum += abs(v-p);
}
cout << sum << "\n";
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:48:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 0; i < a.size()/2; i++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
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 |
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 |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
440 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 |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
24 ms |
1524 KB |
Output is correct |
13 |
Correct |
56 ms |
1484 KB |
Output is correct |
14 |
Correct |
44 ms |
1484 KB |
Output is correct |
15 |
Correct |
58 ms |
976 KB |
Output is correct |
16 |
Correct |
39 ms |
1480 KB |
Output is correct |
17 |
Correct |
48 ms |
1480 KB |
Output is correct |
18 |
Correct |
49 ms |
1480 KB |
Output is correct |
19 |
Correct |
73 ms |
1364 KB |
Output is correct |
20 |
Correct |
53 ms |
1432 KB |
Output is correct |
21 |
Correct |
63 ms |
1516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
600 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |