This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// ioi-17-p2-wiring.yml
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
long long min_total_length(std::vector<int> r, std::vector<int> b) {
if (r.size() > b.size()) r.swap(b);
long long res = 0;
set<int> bs(b.begin(), b.end());
bs.insert(-INF); bs.insert(INF);
for (int i = 0; i < (int)r.size(); i++) {
auto nx = bs.lower_bound(r[i]), pr = nx; pr--;
// cout<<"i="<<(i)<<", "; cout<<"r[i]="<<(r[i])<<", "; cout<<"*pr="<<(*pr)<<", "; cout<<"*nx="<<(*nx)<<", "; cout << endl; // db i,r[i],*pr,*nx
int mn = INT_MAX;
if (*nx != INF)
mn = min(mn, *nx - r[i]);
bool usepr = false;
if (*pr != -INF) { // prioritize left over right
int alt = r[i] - *pr;
if (alt <= mn) {
mn = alt;
usepr = true;
bs.erase(pr);
}
}
if (!usepr)
bs.erase(nx);
// cout<<"mn="<<(mn)<<", "; cout << "bs=["; for (auto x:bs)cout<<x<<", "; cout<<"], "; cout << endl; // db mn,I:bs
res += mn;
}
r.insert(r.begin(), -INF);
r.push_back(INF);
for (auto x : bs) {
if (abs(x) >= INF) continue;
auto nx = lower_bound(r.begin(), r.end(), x), pr = nx - 1;
int mn = INT_MAX;
if (*nx != INF)
mn = min(mn, *nx - x);
if (*pr != -INF)
mn = min(mn, x - *pr);
res += mn;
}
return res;
}
#ifdef LOCAL
int main() {
long long res = min_total_length({1, 2, 3, 7}, {0, 4, 5, 9, 10});
cout<<"[ANSWER]: "; cout<<"res="<<(res)<<", "; cout << endl; // db l:ANSWER,res
return 0;
}
#endif
# | 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... |