Submission #258539

#TimeUsernameProblemLanguageResultExecution timeMemory
258539Plasmatic전선 연결 (IOI17_wiring)C++11
13 / 100
57 ms4328 KiB
// 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...