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"
#define ll long long int
#define pb push_back
#define pii pair<int,int>
#define ff first
#define ss second
#define sz size()
const int N = 2e5 + 1;
using namespace std;
int n, k, a[N], b[N];
ll ans, p[N];
char s[N], d[N];
vector <ll> v;
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> k >> n;
v.pb(0);
for(int i = 1; i <= n; i++){
cin >> s[i] >> a[i] >> d[i] >> b[i];
if(s[i] == d[i]) ans += abs(a[i] - b[i]);
else{
v.pb(a[i]);
v.pb(b[i]);
}
}
sort(v.begin(), v.end());
int n1 = v.sz-1;
for(int i = 1; i <= n1; i++) v[i] += v[i-1];
ans += n1 / 2;
ll mn = 1e15;
if (n1 == 0) mn = 0;
if(k == 1){
for(int i = 1; i <= n1; i++){
int nd = v[i] - v[i-1];
ll san = (i*nd)-v[i] + (v.back() - v[i-1]) - ((n1-i+1)*nd);
mn = min(mn,san);
}
cout << ans + mn << '\n';
}
else{
for(int i = 1; i <= n1; i++){
for(int j = i+1; j <= n1; j++){
int nd = v[i] - v[i-1], nd1 = v[j] - v[j-1];
ll san = 0;
for(int u = 1; u <= n; u++){
if(s[u] == d[u]) continue;
san += min(abs(a[u]-nd)+abs(b[u]-nd),abs(a[u]-nd1)+abs(b[u]-nd1));
}
mn = min(mn,san);
}
}
cout << ans + mn << '\n';
}
}
# | 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... |