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 pll pair<ll, ll>
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int k, n;
cin >> k >> n;
ll sum = 0;
vector<pll> se;
for (int i = 0; i < n; i++) {
ll s, t;
char p, q;
cin >> p >> s >> q >> t;
sum += abs(s-t);
if (p != q) sum++;
else continue;
if (s > t) swap(s, t);
se.push_back(make_pair(s, 0));
se.push_back(make_pair(t, 1));
}
n = (int) se.size()/2;
sort(se.begin(), se.end());
vector<ll> dpe(2*n, 0);
ll ae = 0;
for (int i = 1; i < 2*n; i++) {
dpe[i] = dpe[i-1];
if (se[i].first == se[i-1].first) {
if (se[i].second == 1) ae++;
continue;
}
dpe[i] += 2*(se[i].first-se[i-1].first)*ae;
if (se[i].second == 1) ae++;
}
vector<ll> dps(2*n, 0);
ll as = 0;
for (int i = 2*n-2; i >= 0; i--) {
dps[i] = dps[i+1];
if (se[i].first == se[i+1].first) {
if (se[i].second == 0) as++;
continue;
}
dps[i] += 2*(se[i+1].first-se[i].first)*as;
if (se[i].second == 0) as++;
}
ll mini = numeric_limits<ll>::max()/2;
if (n == 0) mini = 0;
for (int i = 0; i < 2*n; i++) {
mini = min(mini, dps[i]+dpe[i]);
}
cout << sum+mini;
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... |