#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define ff first
#define ss second
#define pii pair<int, int>
ll f(int x, vector<pii> &v) {
ll sm = 0;
for (auto i : v) sm += abs(i.ff - x) + 1 + abs(i.ss - x);
return sm;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int k, n;
cin >> k >> n;
ll sm = 0;
vector<pii> v;
for (int i = 0; i < n; i++) {
char p, q;
int s, t;
cin >> p >> s >> q >> t;
if (p == q) sm += abs(t - s);
else v.push_back({s, t});
}
ll ans = LLONG_MAX;
for (int i = 0; i < (int)v.size(); i++) {
vector<pii> u1, u2;
for (int j = 0; j <= i; j++) u1.push_back(v[j]);
for (int j = i + 1; j < (int)v.size(); j++) u2.push_back(v[j]);
int l = 0, r = (int)1e9;
while (l <= r) {
int md = (l + r) >> 1;
if (f(md, u1) >= f(md + 1, u1)) l = md + 1;
else r = md - 1;
}
int l1 = 0, r1 = (int)1e9;
while (l1 <= r1) {
int md = (l1 + r1) >> 1;
if (f(md, u2) >= f(md + 1, u2)) l1 = md + 1;
else r1 = md - 1;
}
ans = min(ans, f(l, u1) + f(l1, u2));
}
cout << sm + ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |