#include <bits/stdc++.h>
#define int long long
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
double l(pair<double,pair<double,double>>a) {
return (double)a.second.first;
}
double r(pair<double,pair<double,double>> a) {
return (double)a.second.second;
}
double v(pair<double,pair<double,double>> a) {
return (double)a.first;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int n,k; cin >> k >> n;
int sum=0;
vector<pair<double,pair<double,double>>> a;
for (int i = 0; i < n; i++){
char p,q; int s,t; cin >> p >> s >> q >> t;
if(s>t) swap(s,t);
if(p!=q) {
a.push_back({((double)s+(double)t)/2,{(double)s,(double)t}});
sum++;
}
sum+=abs(s-t);
}
n=sz(a);
sort(all(a));
int mn=1e18;
if(n==0){
cout << sum << "\n";
return 0;
}
if(k==1){
mn=0;
int md=(v(a[(n-1)/2])+v(a[(n)/2]))/2;
for (int j = 0; j < n; j++)
{
mn+=max((double)0,max(l(a[j])-md,md-r(a[j])))*2;
}
}else{
for (int i = 0; i <= n; i++)
{
int sm=0;
int md=0;
if(i>0) md=(v(a[(i-1)/2])+v(a[i/2]))/2;
for (int j = 0; j < i; j++)
{
sm+=max((double)0,max(l(a[j])-md,md-r(a[j])))*2;
}
md=0;
if(i<n) md=(v(a[(i+n-1)/2])+v(a[(i+n)/2]))/2;
for (int j = 0; j < i; j++)
{
sm+=max((double)0,max(l(a[j])-md,md-r(a[j])))*2;
}
mn=min(mn,sm);
}
}
cout << sum+mn << "\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... |