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 all(x) x.begin(),x.end()
#define pii pair<int, int>
#define ft first
#define sd second
#define MP make_pair
#define PB push_back
#define sz(x) ((int)x.size())
using namespace std;
typedef long long ll;
const ll PW = 43;
const int N = 30010;
const int B = 100;
const int oo = 2e9;
const ll OO = 1e18;
vector<int> pts, segl, segr;
ll extra = 0, best = OO, kl = 0, kr = 0, sl = 0, sr = 0;
int k, n, siz;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("in.txt","r",stdin);
cin >> k >> n;
assert(k == 1);
for (int i = 0; i < n; i++){
char c1, c2; int l, r;
cin >> c1 >> l >> c2 >> r;
if (l > r) swap(l, r);
extra += r - l;
if (c1 != c2){
extra++;
segl.PB(l);
segr.PB(r);
pts.PB(l);
pts.PB(r);
}
}
sort(all(pts));
pts.resize(unique(all(pts)) - pts.begin());
sort(all(segl));
sort(all(segr));
siz = sz(segr);
for (int i = 0; i < siz; i++){
kl++;
sl += segl[i];
}
for (int x : pts){
while (kl > 0 && segl[siz - kl] <= x){
sl -= segl[siz - kl];
kl--;
}
while (kr < siz && segr[kr] < x){
sr += segr[kr];
kr++;
}
ll cur = sl - (ll(x) * kl) + (ll(x) * kr) - sr;
best = min(best, cur + cur);
}
if (best == OO)
best = 0;
cout << best + extra;
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... |