#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll base_cost;
int n, k;
vector<pair<int, int>> paths;
ll check(int firstbridge, bool skip = false) {
// all paths that start on the left of firstbridge must use it
// so we only care about paths entirely to the right
// for each of those paths, keep track of when cost stops decreasing,
// and when it starts increasing
ll overhead = 0;
ll diff = 0;
map<int, ll> dx;
for (int i = 0; i < n; i ++) {
if (paths[i].second < firstbridge || paths[i].first > firstbridge)
overhead += (ll)abs(firstbridge - paths[i].first) + abs(firstbridge - paths[i].second);
else
overhead += (ll)abs(paths[i].second - paths[i].first);
if (skip || paths[i].first > firstbridge) {
diff -= 2;
dx[paths[i].first] += 2;
dx[paths[i].second] += 2;
if (!skip) {
ll min_eq = paths[i].second + (paths[i].first - firstbridge);
if (min_eq <= 1000000000)
dx[min_eq] -= 2;
}
}
}
int last = firstbridge;
ll best = overhead;
for (auto a : dx) {
int curdist = a.first, dd = a.second;
overhead += (ll)(curdist - last) * diff;
diff += dd;
best = min(best, overhead);
// cout << a.first << " " << overhead + base_cost << endl;
last = curdist;
}
return best;
}
int main() {
cin >> k >> n;
base_cost = 0;
for (int i = 0; i < n; i ++) {
char a, b;
int x, y;
cin >> a >> x >> b >> y;
if (a == b) {
base_cost += abs(y - x);
} else {
base_cost ++;
paths.push_back({min(x, y), max(x, y)});
}
}
n = paths.size();
sort(paths.begin(), paths.end());
if (n == 0) {
cout << base_cost << endl; return 0;
}
if (k == 1) {
cout << check(0, true) + base_cost << endl; return 0;
}
// brute force
ll best = 1e18;
for (int i = 0; i < n; i ++) {
best = min(best, check(paths[i].first, false));
best = min(best, check(paths[i].second, false));
}
cout << best + base_cost << endl; return 0;
}
/*
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
# |
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 |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
472 KB |
Output is correct |
7 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 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 |
2 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 |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
444 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
24 ms |
2116 KB |
Output is correct |
13 |
Correct |
107 ms |
15812 KB |
Output is correct |
14 |
Correct |
45 ms |
2756 KB |
Output is correct |
15 |
Correct |
58 ms |
9412 KB |
Output is correct |
16 |
Correct |
37 ms |
2772 KB |
Output is correct |
17 |
Correct |
79 ms |
15972 KB |
Output is correct |
18 |
Correct |
86 ms |
15512 KB |
Output is correct |
19 |
Correct |
88 ms |
15244 KB |
Output is correct |
20 |
Correct |
42 ms |
3176 KB |
Output is correct |
21 |
Correct |
84 ms |
15664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
2 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 |
432 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
448 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 |
4 ms |
348 KB |
Output is correct |
14 |
Correct |
39 ms |
436 KB |
Output is correct |
15 |
Correct |
149 ms |
448 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
17 ms |
348 KB |
Output is correct |
18 |
Correct |
23 ms |
604 KB |
Output is correct |
19 |
Correct |
6 ms |
468 KB |
Output is correct |
20 |
Correct |
172 ms |
604 KB |
Output is correct |
21 |
Correct |
69 ms |
348 KB |
Output is correct |
22 |
Correct |
184 ms |
600 KB |
Output is correct |
23 |
Correct |
5 ms |
348 KB |
Output is correct |
24 |
Correct |
130 ms |
348 KB |
Output is correct |
# |
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 |
2 ms |
436 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
4 ms |
452 KB |
Output is correct |
14 |
Correct |
37 ms |
344 KB |
Output is correct |
15 |
Correct |
176 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
13 ms |
456 KB |
Output is correct |
18 |
Correct |
27 ms |
344 KB |
Output is correct |
19 |
Correct |
3 ms |
348 KB |
Output is correct |
20 |
Correct |
176 ms |
604 KB |
Output is correct |
21 |
Correct |
65 ms |
560 KB |
Output is correct |
22 |
Correct |
201 ms |
604 KB |
Output is correct |
23 |
Correct |
5 ms |
344 KB |
Output is correct |
24 |
Correct |
149 ms |
348 KB |
Output is correct |
25 |
Execution timed out |
2023 ms |
2256 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |