Submission #832170

#TimeUsernameProblemLanguageResultExecution timeMemory
832170skittles1412Wiring (IOI17_wiring)C++17
7 / 100
1075 ms7032 KiB
#include "bits/extc++.h"

using namespace std;

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t;
    ((cerr << " | " << u), ...);
    cerr << endl;
}

#ifdef DEBUG
#define dbg(...)                                              \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
    dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr   \
    if (false) \
    cerr
#endif

using ll = long long;

#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))

ll min_total_length(vector<int> arr_r, vector<int> arr_b) {
    vector<pair<int, bool>> arr;
    for (auto& a : arr_r) {
        arr.emplace_back(a, false);
    }
    for (auto& a : arr_b) {
        arr.emplace_back(a, true);
    }

    sort(begin(arr), end(arr));

    int n = sz(arr);

    long dp[n + 1];
    memset(dp, 0x3f, sizeof(dp));
    dp[n] = 0;

    deque<int> inds[2];

    for (int i = n - 1; i >= 0; i--) {
        if (i + 1 < n) {
            inds[arr[i + 1].second].push_front(i + 1);
        }
        auto [t, ty] = arr[i];

        auto &me = inds[ty], &other = inds[!ty];

        auto get_w = [&](int u, int v) -> long {
            return abs(arr[u].first - arr[v].first);
        };
        auto get = [&](const deque<int>& arr, int ind) -> int {
            if (ind >= sz(arr)) {
                return n;
            }
            return arr[ind];
        };

        if (!sz(other)) {
            continue;
        }

        if (get(other, 0) < get(me, 0)) {
            long csum = 0;

            for (int j = 0; j < sz(other) && get(other, j) < get(me, 0); j++) {
                csum += get_w(i, other[j]);
                dp[i] = min(dp[i], csum + min(dp[other[j]], dp[other[j] + 1]));
            }
        } else {
            dp[i] = dp[i + 1] + get_w(i, other[0]);

            long csum = 0;

            me.push_front(i);
            for (int j = 0; j < sz(other) && j < sz(me); j++) {
                long xsum = csum;
                for (int k = j; k < sz(other); k++) {
                    xsum += get_w(me[j], other[k]);
                    dp[i] =
                        min(dp[i],
                            xsum + dp[min(get(other, k + 1), get(me, j + 1))]);
                }
                csum += get_w(me[j], other[j]);
            }
            me.pop_front();
        }
    }

    return dp[0];
}
#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...