이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// 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... |