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 <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
const int mxn = 100000;
int n, m, k;
pi a[mxn];
ll f[2][mxn];
priority_queue<int> l;
priority_queue<int, vector<int>, greater<int>> r;
void add(int x){
if(l.empty() || x <= l.top()) l.push(x);
else r.push(x);
while(l.size() < r.size()) l.push(r.top()), r.pop();
while(r.size() + 1 < l.size()) r.push(l.top()), l.pop();
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> k >> n;
ll s = 0;
for(int i = 0; i < n; i++){
char c, d;
int x, y;
cin >> c >> x >> d >> y;
if(x > y) swap(x, y);
s += y - x;
if(c != d) s++, a[m++] = {x, y};
}
sort(a, a + m, [&](pi x, pi y){
return x.f + x.s < y.f + y.s;
});
for(int t = 0; t < 2; t++){
while(!l.empty()) l.pop();
while(!r.empty()) r.pop();
for(int i = 0; i < m; i++){
add(a[i].f), add(a[i].s);
for(int j = 0; j < i; j++){
f[t][i] += max(0, a[j].f - l.top()) + max(0, l.top() - a[j].s);
}
}
reverse(a, a + m);
}
reverse(f[1], f[1] + m);
ll ret = 0x3f3f3f3f3f3f3f3f;
if(k & 1) ret = f[1][0];
else for(int i = 1; i < m; i++) ret = min(ret, f[0][i - 1] + f[1][i]);
ret = 2 * ret + s;
cout << ret << endl;
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... |