#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
using namespace std;
int main() {
int K, n;
cin >> K >> n;
vector<pair<long long, long long>> citizens;
long long basicCost = 0;
for (int i = 0;i < n; ++i) {
long long x, y;
char a, b;
cin >> a >> x >> b >> y;
if (a == b) {
basicCost += abs(x-y);
} else {
basicCost += 1;
if (x > y) citizens.push_back({y, x});
else citizens.push_back({x, y});
}
}
sort(citizens.begin(), citizens.end(), [](const pair<long long, long long>& a, const pair<long long, long long>& b) {
return a.first + a.second < b.first + b.second;
});
vector<array<long long, 3>> points;
points.push_back({0, -1, -1}); // add a fake point for easy boundary handling.
for (int i = 0; i < citizens.size(); ++i) {
points.push_back({citizens[i].first, 0, i});
points.push_back({citizens[i].second, 1, i});
}
sort(points.begin(), points.end());
/*
cerr << "init, bcost:" << basicCost << " citizens:" << citizens.size() << endl;
for (auto p : points) {
cerr << "dist:" << p[0] << " pt:" << p[1] << " idx:" << p[2] << endl;
}
*/
long long rightCost = 0;
for (auto& ctz : citizens) {
rightCost += ctz.first + ctz.second;
}
int rightIndex = 0;
int rightBehind = 0, rightAhead = citizens.size();
while (rightBehind < rightAhead) {
++rightIndex;
rightCost += (rightBehind - rightAhead) * 2 * (points[rightIndex][0] - points[rightIndex - 1][0]);
if (points[rightIndex][1] == 0) rightAhead -= 1;
else if (points[rightIndex][1] == 1) rightBehind += 1;
}
if (K == 1) {
cout << rightCost + basicCost << endl;
return 0;
}
long long bestCost = rightCost;
long long leftCost = 0;
int leftIndex = 0, leftAhead = 0, leftBehind = 0;
for (int mid = 0; mid + 1 < citizens.size(); ++mid) {
// cerr << "----- " << "round " << mid << endl;
// Reduce the effect of mid from right side.
if (array<long long, 3>({citizens[mid].first, 0, mid}) > points[rightIndex]) {
rightAhead -= 1;
rightCost -= citizens[mid].first + citizens[mid].second - 2 * points[rightIndex][0];
} else if (array<long long, 3>({citizens[mid].second, 1, mid}) <= points[rightIndex]) {
rightBehind -= 1;
rightCost -= - (citizens[mid].first + citizens[mid].second) + 2 * points[rightIndex][0];
} else {
rightCost -= citizens[mid].second - citizens[mid].first;
}
while (rightBehind < rightAhead) {
rightIndex += 1;
rightCost += (rightBehind - rightAhead) * 2 * (points[rightIndex][0] - points[rightIndex - 1][0]);
if (points[rightIndex][2] > mid) {
if (points[rightIndex][1] == 0) rightAhead -= 1;
else rightBehind += 1;
}
}
// Add the cost of newly added stuff.
if (array<long long, 3>({citizens[mid].first, 0, mid}) > points[leftIndex]) {
leftAhead += 1;
leftCost += citizens[mid].first + citizens[mid].second - 2 * points[leftIndex][0];
} else if (array<long long, 3>({citizens[mid].second, 1, mid}) <= points[leftIndex]) {
leftBehind += 1;
leftCost += - (citizens[mid].first + citizens[mid].second) + 2 * points[leftIndex][0];
} else {
leftCost += citizens[mid].second - citizens[mid].first;
}
while (leftBehind < leftAhead) {
leftIndex += 1;
leftCost += (leftBehind - leftAhead) * 2 * (points[leftIndex][0] - points[leftIndex - 1][0]);
if (points[leftIndex][2] <= mid) {
if (points[leftIndex][1] == 0) leftAhead -= 1;
else leftBehind += 1;
}
}
/*
cerr << "Round: " << mid << endl;
cerr << rightCost<< ": " << rightIndex << " " << rightAhead << ":" << rightBehind << endl;
cerr << leftCost << ": " << leftIndex << " " << leftAhead << ":" << leftBehind << endl;
*/
if (leftCost + rightCost < bestCost) {
bestCost = leftCost + rightCost;
}
}
cout << bestCost + basicCost << endl;
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:30:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for (int i = 0; i < citizens.size(); ++i) {
| ~~^~~~~~~~~~~~~~~~~
bridge.cpp:62:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int mid = 0; mid + 1 < citizens.size(); ++mid) {
| ~~~~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
560 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
2 ms |
508 KB |
Output is correct |
6 |
Correct |
2 ms |
336 KB |
Output is correct |
7 |
Correct |
2 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
336 KB |
Output is correct |
11 |
Correct |
2 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
2 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Correct |
2 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
336 KB |
Output is correct |
11 |
Correct |
2 ms |
336 KB |
Output is correct |
12 |
Correct |
63 ms |
11956 KB |
Output is correct |
13 |
Correct |
97 ms |
13492 KB |
Output is correct |
14 |
Correct |
63 ms |
12212 KB |
Output is correct |
15 |
Correct |
59 ms |
6936 KB |
Output is correct |
16 |
Correct |
90 ms |
12704 KB |
Output is correct |
17 |
Correct |
76 ms |
13432 KB |
Output is correct |
18 |
Correct |
71 ms |
12996 KB |
Output is correct |
19 |
Correct |
76 ms |
13492 KB |
Output is correct |
20 |
Correct |
73 ms |
12980 KB |
Output is correct |
21 |
Correct |
76 ms |
13084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
2 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
444 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
504 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
2 ms |
336 KB |
Output is correct |
14 |
Correct |
2 ms |
336 KB |
Output is correct |
15 |
Correct |
3 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
2 ms |
336 KB |
Output is correct |
18 |
Correct |
2 ms |
336 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
448 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
508 KB |
Output is correct |
7 |
Correct |
1 ms |
440 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
504 KB |
Output is correct |
25 |
Correct |
55 ms |
11776 KB |
Output is correct |
26 |
Correct |
65 ms |
11944 KB |
Output is correct |
27 |
Correct |
77 ms |
12728 KB |
Output is correct |
28 |
Correct |
95 ms |
13492 KB |
Output is correct |
29 |
Correct |
115 ms |
13476 KB |
Output is correct |
30 |
Correct |
48 ms |
6752 KB |
Output is correct |
31 |
Correct |
67 ms |
12724 KB |
Output is correct |
32 |
Correct |
69 ms |
13492 KB |
Output is correct |
33 |
Correct |
81 ms |
12980 KB |
Output is correct |
34 |
Correct |
75 ms |
13484 KB |
Output is correct |
35 |
Correct |
72 ms |
12972 KB |
Output is correct |
36 |
Correct |
69 ms |
13276 KB |
Output is correct |
37 |
Correct |
51 ms |
12004 KB |
Output is correct |