Submission #832179

#TimeUsernameProblemLanguageResultExecution timeMemory
832179skittles1412Wiring (IOI17_wiring)C++17
17 / 100
1087 ms7704 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]);

            {
                int j, cnt = 0;
                long wsum = 0, last_m;
                for (j = i; j < n; j++) {
                    if (arr[i].second == arr[j].second) {
                        wsum -= arr[j].first;
                        last_m = arr[j].first;
                        cnt++;
                    } else {
                        wsum += arr[j].first;
                        cnt--;
                    }
                    if (!cnt) {
                        break;
                    }
                }

                if (!cnt) {
                    for (; j < n; j++) {
                        if (arr[i].second == arr[j].second) {
                            break;
                        }

                        dp[i] = min(dp[i], wsum + min(dp[j], dp[j + 1]));

                        if (j + 1 < n) {
                            wsum += arr[j + 1].first - last_m;
                        }
                    }
                }
            }

            // 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];
}

Compilation message (stderr)

wiring.cpp: In function 'll min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:106:54: warning: 'last_m' may be used uninitialized in this function [-Wmaybe-uninitialized]
  106 |                             wsum += arr[j + 1].first - last_m;
#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...