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;
long long INF = 1e18;
vector<int> cp;
vector<pair<int, int>> vec;
int k, n;
int main(){
cin >> k >> n;
long long ans = INF;
long long base = 0;
for(int i = 0; i < n; i++){
char t1, t2;
pair<int, int> p;
cin >> t1 >> p.first >> t2 >> p.second;
if(p.first > p.second)swap(p.first, p.second);
base += p.second - p.first;
if(t1 != t2) {
base++;
vec.push_back(p);
cp.push_back(p.first);
cp.push_back(p.second);
}
}
sort(cp.begin(), cp.end());
cp.erase(unique(cp.begin(), cp.end()), cp.end());
for(auto x: cp){
for(auto y: cp){
long long sum = 0;
for(auto p: vec){
if(p.first <= x && x <= p.second)continue;
if(p.first <= y && y <= p.second)continue;
sum += min(min(abs(x - p.first), abs(x - p.second)), min(abs(y - p.first), abs(y - p.second)));
}
ans = min(ans, sum);
// cout << x << " " << y << " " << sum << endl;
}
}
cout << base + ans*2;
}
# | 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... |