제출 #248727

#제출 시각아이디문제언어결과실행 시간메모리
248727MarcoMeijerWiring (IOI17_wiring)C++14
13 / 100
1095 ms12704 KiB
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;

//macros
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(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(int a=int(c-1); a>=int(b); a--)
#define INF 1e18
#define pb push_back
#define fi first
#define se second
#define sz size()

const int MX = 1e6;

vi 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[max(0ll,ed-1)];
    if(cent < bg || cent == 0) return INF;
    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]*(right - left);
    }
    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;
        REV(j,0,i) {
            dp[i] = min(dp[i], dp[j] + getCost(j,i-1));
        }
    }

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