이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// ioi-17-p2-wiring.yml
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pib = pair<int, bool>;
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;
vector<pib> vs;
for (auto x : r) vs.emplace_back(x, false);
for (auto x : b) vs.emplace_back(x, true);
sort(vs.begin(), vs.end());
deque<int> bstk, rneed;
for (auto x : vs) {
// cout<<"x.first="<<(x.first)<<", "; cout<<"x.second="<<(x.second)<<", "; cout << "bstk=["; for (auto x:bstk)cout<<x<<", "; cout<<"], "; cout << "rneed=["; for (auto x:rneed)cout<<x<<", "; cout<<"], "; cout << endl; // db x.first,x.second,I:bstk,I:rneed
if (!x.second) {
if (bstk.empty())
rneed.push_back(x.first);
else {
res += abs(x.first - bstk.back());
bstk.pop_back();
}
}
else {
if (!rneed.empty()) {
res += abs(rneed.front() - x.first);
rneed.pop_front();
}
else
bstk.push_back(x.first);
}
}
r.insert(r.begin(), -INF);
r.push_back(INF);
for (auto x : bstk) {
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);
// cout<<"x="<<(x)<<", "; cout<<"mn="<<(mn)<<", "; cout<<"*pr="<<(*pr)<<", "; cout<<"*nx="<<(*nx)<<", "; cout << endl; // db x,mn,*pr,*nx
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... |