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>
#define pii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()
#define vec vector<int>
#define dead not_bad
#define bad gooood
#define left not_right
#define y1 what
using namespace std;
const int N = (int) 2e5 + 10;
const int M = (int) 2e6;
const ll LINF = (ll) 2e18;
const int INF = (int) 1e9 + 7;
const int ALPHA = 26;
const int mod = INF;
const double PI = 3.14159265359;
const ld EPS = (ld) 1e-12;
const int nx[4] = {0, 0, -1, 1};
const int ny[4] = {1, -1, 0, 0};
ll solve(vec a, vec b) {
vector<pair<int, ll> > arr[2];
int n = sz(a);
for (int i = 0; i < n; i++) {
arr[0].pb(mp(min(a[i], b[i]), a[i] + b[i]));
arr[1].pb(mp(max(a[i], b[i]), a[i] + b[i]));
}
for (int iter = 0; iter < 2; iter++) {
sort(all(arr[iter]));
for (int i = 1; i < n; i++) {
//arr[iter][i].s += arr[iter][i - 1].s;
}
}
vec x_arr;
for (auto it : a) {
x_arr.pb(it);
}
for (auto it : b) {
x_arr.pb(it);
}
/*
for (auto it : a) {
cout << it << ' ';
}
cout << endl;
for (auto it : b) {
cout << it << ' ';
}
cout << endl;
*/
ll ans = LINF;
for (auto x : x_arr) {
//if (x != 4) continue;
ll cur = 0;
/*
for (auto it : arr[0]) {
if (it.f > x) {
cur += it.s - 2 * x;
}
}
//cout << cur << ' '; cur = 0;
for (auto it : arr[1]) {
if (it.f < x) {
cur += 2 * x - it.s;
}
}
*/
//cout << cur << ' '; cur = 0;
for (int i = 0; i < n; i++) {
if (a[i] <= x && x <= b[i]) {
cur += b[i] - a[i];
} else if (min(a[i], b[i]) > x) {
cur += a[i] + b[i] - 2 * x;
} else if (max(a[i], b[i]) < x) {
cur += 2 * x - (a[i] + b[i]);
} else {
assert(0);
}
}
ans = min(ans, cur);
//cout << cur << endl;
}
return ans + n;
}
int k, n;
int a[N], b[N];
int main() {
#define fn "teams"
#ifdef witch
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
//freopen(fn".in", "r", stdin);
//freopen(fn".out", "w", stdout);
#endif
int nn;
ll ans = 0;
cin >> k >> nn;
vec A, B;
for (int i = 1; i <= nn; i++) {
char ta, tb;
int x1, x2;
cin >> ta >> x1 >> tb >> x2;
//cout << ta << ' ' << x1 << ' ' << tb << ' ' << x2 << endl;
if (ta == tb) {
ans += abs(x2 - x1);
} else {
if (x1 > x2)
swap(x1, x2);
++n;
a[n] = x1;
b[n] = x2;
A.pb(a[n]);
B.pb(b[n]);
}
}
cout << ans + solve(A, B);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |