Submission #1336394

#TimeUsernameProblemLanguageResultExecution timeMemory
1336394anteknneWiring (IOI17_wiring)C++20
100 / 100
45 ms9500 KiB
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;

#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define debug false

const ll INF = ll(1000 * 1000 * 1000) * ll(1000 * 1000 * 1000);
const int MAXN = 200 * 1000 + 17;
vector<pii> v;
vector<int> blok;
ll dp[MAXN];
ll spref[MAXN];
vector<ll> minpref;
vector<ll> minsuf;

ll min_total_length (vector<int> r, vector<int> b) {
    for (auto x : r) {
        v.pb({x, 0});
    }
    for (auto x : b) {
        v.pb({x, 1});
    }
    v.pb({-1, -1});
    sort(v.begin(), v.end());
    for (int i = 1; i < int(v.size()); i ++) {
        spref[i] = spref[i - 1] + v[i].st;
    }

    blok.pb(1);
    for (int i = 2; i < int(v.size()); i ++) {
        if (v[i].nd != v[i - 1].nd) {
            blok.pb(i);
        }
    }

    int n = int(blok.size());
    blok.pb(int(v.size()));

    for (int i = 1; i < blok[1]; i ++) {
        dp[i] = INF;
    }

    for (int b = 1; b < n; b ++) {
        int l = blok[b];
        int ind = blok[b - 1] - 1;
        minpref.clear();
        minsuf.clear();

        for (int j = ind; j <= l - 1; j ++) {
            ll x = dp[j] + spref[j] - ll(j) * v[l].st;
            if (minpref.empty()) {
                minpref.pb(x);
            } else {
                minpref.pb(min(x, minpref.back()));
            }
        }

        for (int j = l - 1; j >= ind; j --) {
            ll x = dp[j] + spref[j] - ll(j) * v[l - 1].st;
            if (minsuf.empty()) {
                minsuf.pb(x);
            } else {
                minsuf.pb(min(x, minsuf.back()));
            }
        }

        reverse(minsuf.begin(), minsuf.end());

        for (int i = l; i < blok[b + 1]; i ++) {
            dp[i] = INF;
            int j = 2 * l - i - 2;
            if (j >= ind) {
                dp[i] = min(dp[i], spref[i] - 2LL * spref[l - 1] + ll(2 * l - i - 2) * v[l].st + minpref[min(j, blok[b] - 1) - ind]);
            }
            if (j <= blok[b] - 1) {
                dp[i] = min(dp[i], spref[i] - 2LL * spref[l - 1] - ll(i - 2 * l + 2) * v[l - 1].st + minsuf[max(j, ind) - ind]);
            }
        }

    }
    return dp[int(v.size()) - 1];
}

/*int main () {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cout << min_total_length({1, 2, 3, 7}, {0, 4, 5, 9, 10});
    return 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...