# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
366323 | dennisstar | Meetings (IOI18_meetings) | C++17 | 0 ms | 0 KiB |
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 "wiring.h"
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
using ll = long long;
const ll INF = 1e18;
ll min_total_length(vector<int> R, vector<int> B) {
int N, M;
N=R.size();
M=B.size();
vector<pair<int, int>> P;
for (auto &i:R) P.emplace_back(i, 1);
for (auto &i:B) P.emplace_back(i, -1);
sort(P.begin(), P.end());
reverse(R.begin(), R.end());
reverse(B.begin(), B.end());
int x=M;
ll r=-INF, b=-INF, D=0, S=0;
vector<pair<ll, ll>> lst(N+M+1, {INF, INF});
lst[M]={0, 0};
for (auto &i:P) {
ll E=0;
(i.se==1?R:B).pop_back();
x+=i.se;
S+=i.fi*i.se;
if (i.se==1) {
r=i.fi;
E=D+i.fi-b;
if (B.size()) E=min(E, D+B.back()-i.fi);
}
if (i.se==-1) {
b=i.fi;
E=D+i.fi-r;
if (R.size()) E=min(E, D+R.back()-i.fi);
}
D=min(E, lst[x].fi+abs(lst[x].se-S));
lst[x]={D, S};
}
return D;
}