Submission #248735

#TimeUsernameProblemLanguageResultExecution timeMemory
248735MarcoMeijerWiring (IOI17_wiring)C++14
20 / 100
1091 ms15328 KiB
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;

//macros
typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ll, ll> lll;
typedef tuple<ll, ll, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(ll a=ll(b); a<ll(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(ll a=ll(c-1); a>=ll(b); a--)
#define INF 1e18
#define pb push_back
#define fi first
#define se second
#define sz size()

const ll MX = 1e6;

vector<int> f[2];
ll N[2], n = 0;
ll a[MX], b[MX], sm[MX], ch[MX], CENT[MX];
ll dp[MX];

ll getCost(ll bg, ll ed) {
    if(ch[ed] - ch[bg] != 1) return INF;
    ll cent = CENT[ed];
    ll cost = 0;
    REP(i,bg  ,cent) cost -= a[i];
    REP(i,cent,ed+1) cost += a[i];
    ll left = cent - bg;
    ll right = ed+1 - cent;
    if(left > right) {
        cost += a[cent  ]*(left - right);
    } else {
        cost += a[cent-1]*(left - right);
    }
    return cost;
}

ll min_total_length(std::vector<int> R, std::vector<int> B) {
    f[0] = R; f[1] = B;
    RE(i,2) N[i]=f[i].sz;

    // fill a and b
    priority_queue<ii, vii, greater<ii>> pq;
    RE(i,2) RE(j,N[i]) pq.push({f[i][j], i});
    while(!pq.empty()) {
        ii p = pq.top(); pq.pop();
        a[n] = p.fi; b[n] = p.se;
        n++;
    }

    sm[0] = 0; ch[0] = 0; CENT[0] = 0;
    REP(i,1,n+1) {
        ch[i] = ch[i-1];
        sm[i] = sm[i-1]+a[i-1];
        CENT[i] = CENT[i-1];
        if(b[i] != b[i-1]) ch[i]++, CENT[i]=i;
    }

    // subt 2
    if(getCost(0, n-1) != INF) return getCost(0, n-1);

    dp[0] = 0;
    REP(i,1,n+1) {
        dp[i] = INF;
        ll ed = i-1;
        ll cent = CENT[ed];
        ll beg = CENT[max(0ll, cent-1)];
        if(b[i] != b[i-1]) {
            REV(j,beg,cent) dp[j] = min(dp[j], dp[j+1]);
        }
        REV(j,beg,cent) {
            dp[i] = min(dp[i], dp[j] + getCost(j,ed));
        }
    }

    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...