# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
971629 | Halym2007 | Palembang Bridges (APIO15_bridge) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define sz size()
const int N = 1e5 + 5;
ll jogap, a[N], b[N];
int n, k;
char oy[N], of[N];
vector <ll> v;
ll hasapla (ll x, ll y) {
ll ret = 0;
for (int i = 1; i <= n; ++i) {
if (oy[i] == of[i]) {
ret += abs (a[i] - b[i]);
}
else {
ll gosh = abs (a[i] - x) + abs (x - b[i]) + 1;
ll gosh1 = 2e18;
if (y != -1) {
gosh1 = abs (a[i] - y) + abs (y - b[i]) + 1;
}
ret += min (gosh, gosh1);
}
}
return ret;
}
int main () {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// freopen ("input.txt", "r", stdin);
cin >> k >> n;
for (int i = 1; i <= n; ++i) {
cin >> oy[i] >> a[i] >> of[i] >> b[i];
}
// return cout << n << " " << k, 0;
// for (int i = 1; i <= n; ++i) {
// cout << i << " --> " << oy[i] << " " << a[i] << " " << of[i] << " " << b[i] << "\n";
// }
// return 0;
jogap = 2e18;
for (int i = 1; i <= n; ++i) {
v.pb (a[i]);v.pb (b[i]);
}
for (int i = 0; i < 2 * n; ++i) {
if (k > 1)
for (int j = i + 1; j < 2 * n; ++j) {
jogap = min a(jogap, hasapla (v[i], v[j]));
}
else {
jogap = min (jogap, hasapla (v[i], -1));
}
}
cout << jogap;
}