이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
assert(k == 2);
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... |