이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int64_t INF = 1e18;
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int K, n;
cin >> K >> n;
int N = 0;
int64_t ans = 0;
vector<array<int, 2>> v;
vector<int64_t> next;
for(int i = 0; i < n; i++){
char s, d;
int st, dt;
cin >> s >> st >> d >> dt;
ans += int64_t(abs(dt - st));
if(s == d)
continue;
if(st > dt) swap(st, dt);
v.push_back({st, dt});
next.push_back(st), next.push_back(dt);
N++;
}
n = N;
sort(v.begin(), v.end());
sort(next.begin(), next.end());
int64_t inc = INF;
for(int i = 0; i < 2*N; i++){
for(int j = i+1; j < 2*N; j++){
int64_t temp = 0;
for(int k = 0; k < N; k++){
if((v[k][0] <= next[i] && v[k][1] >= next[i]) || (v[k][0] <= next[j] && v[k][1] >= next[i]))
continue;
int64_t tmp = INF;
if(next[i] < v[k][0])
tmp = min(tmp, v[k][0] - next[i]);
if(next[i] > v[k][1])
tmp = min(tmp, next[i] - v[k][1]);
if(next[j] < v[k][0])
tmp = min(tmp, v[k][0] - next[j]);
if(next[j] > v[k][1])
tmp = min(tmp, next[j] - v[k][1]);
temp += tmp;
}
inc = min(inc, temp);
}
}
cout << ans + inc + N << '\n';
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... |