Submission #408638

#TimeUsernameProblemLanguageResultExecution timeMemory
408638wiwihoWiring (IOI17_wiring)C++14
100 / 100
57 ms11772 KiB
#include "wiring.h"

#include <bits/stdc++.h>
#include <bits/extc++.h>

#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
    if(pvaspace) b << " "; pvaspace=true;\
    b << pva;\
}\
b << "\n";}

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;

const ll MOD = 1000000007;
const ll MAX = 1LL << 60;

template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
    return o << '(' << p.F << ',' << p.S << ')';
}

ll ifloor(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a < 0) return (a - b + 1) / b;
    else return a / b;
}

ll iceil(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a > 0) return (a + b - 1) / b;
    else return a / b;
}

ll min_total_length(vector<int> r, vector<int> b) {
//    printv(r, cerr);
//    printv(b, cerr);
//
//    ll tans = 0;
//    for(int i : r) tans += r.back() - i;
//    for(int i : b) tans += i - b.front();
//    tans += (ll)(b.front() - r.back()) * max(r.size(), b.size());
//    cerr << tans << "\n";

    vector<pii> a;
    a.eb(mp(-1, -1));
    for(int i : r) a.eb(mp(i, 0));
    for(int i : b) a.eb(mp(i, 1));

    lsort(a);

    int n = (int)a.size() - 1;

    vector<ll> dp(n + 1, MAX), suf(n + 1, MAX), pre(n + 1, MAX), sum(n + 1, MAX);
    dp[0] = suf[0] = pre[0] = sum[0] = 0;

    int lst = 1;

    int i;
    for(i = 1; i <= n && a[i].S == a[1].S; i++);
    i--;
    ll now = 0;
    for(int j = i; j >= 1; j--){
        now += a[i].F - a[j].F;
        sum[j] = now;
    }
    dp[1] = suf[1];
    suf[1] = dp[0] + sum[1];
    pre[1] = dp[0] + sum[1] + (ll)i * (a[i + 1].F - a[i].F);
    for(int j = 2; j <= i; j++){
        pre[j] = pre[j - 1];
    }
//    cerr << "test " << i << "\n";
//    printv(sum, cerr);

    for(i++; i <= n; i++){
        if(a[i].S == a[i - 1].S) continue;

        now = 0;
        int j;
        for(j = i; j <= n && a[i].S == a[j].S; j++){
            now += a[j].F - a[i].F;
//            cerr << j << " " << suf[max(lst, i - 1 - (j - i))] << " " << i - 1 - (j - i) << " " << (ll)(a[i].F - a[i - 1].F) * (j - i + 1) << " " << now << "\n";
            dp[j] = min(dp[j], suf[max(lst, i - 1 - (j - i))] + (ll)(a[i].F - a[i - 1].F) * (j - i + 1) + now);
            if(i - 1 - (j - i) >= lst){
                dp[j] = min(dp[j], pre[i - 1 - (j - i)] + now);
            }
        }

        lst = i;

        int ed = --j;
        now = 0;
        suf[ed] = dp[ed];
        for(; j >= i; j--){
            now += a[ed].F - a[j].F;
            sum[j] = now;
            suf[j] = min(suf[j], dp[j - 1] + now);
            if(j < ed) suf[j] = min(suf[j], suf[j + 1]);
        }
//        cerr << "test " << i << " " << ed << "\n";

        if(ed == n) continue;

        for(j = i; j <= ed; j++){
            pre[j] = dp[j - 1] + sum[j] + (ll)(a[ed + 1].F - a[ed].F) * (ed - j + 1);
            if(j > i) pre[j] = min(pre[j], pre[j - 1]);
        }

    }

//    printv(a, cerr);
//    printv(dp, cerr);
//    printv(sum, cerr);
//    printv(suf, cerr);
//    printv(pre, cerr);

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