제출 #247657

#제출 시각아이디문제언어결과실행 시간메모리
247657MarcoMeijer전선 연결 (IOI17_wiring)C++14
0 / 100
7 ms640 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 = 1e4+10;

int n[2], p[2], m=0;
vi f[2];
ll a[MX], b[MX];
ll dp[MX], ch[MX];
ll ans=0;

ll getCost(ll bg, ll ed) {
    if(ch[ed]-ch[bg] != 1) return INF;
    ll lb=bg, ub=ed;
    while(lb != ub) {
        ll mid=(lb+ub)/2;
        if(b[mid] == b[bg]) lb=mid+1;
        else ub=mid;
    }
    ll ret = 0;
    REP(i,bg  ,lb  ) ret += a[lb] - a[i   ];
    REP(i,lb+1,ed+1) ret += a[i ] - a[lb-1];
    return ret;
}

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()) {
        a[m] = pq.top().fi;
        b[m] = pq.top().se;
        pq.pop(); m++;
    }

    ch[0]=0;
    REP(i,1,m) {
        ch[i] = ch[i-1];
        if(b[i] != b[i-1]) ch[i]++;
    }

    ans = getCost(0,m-1);
	return ans;
}
#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...