#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){
if(y < x)continue;
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 |
1 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |