제출 #329402

#제출 시각아이디문제언어결과실행 시간메모리
329402cki86201전선 연결 (IOI17_wiring)C++17
100 / 100
42 ms10212 KiB
#include <bits/stdc++.h>
#include "wiring.h"

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define szz(x) ((int)(x).size())
#define rep(i, n) for(int i=0;i<n;i++)
#define all(x) (x).begin(), (x).end()
typedef tuple<int, int, int> t3;
typedef pair<ll, ll> pll;
typedef long double ldouble;
typedef pair<double, double> pdd;

int n, m;
int S[500050], T[500050];
pii p[1000010]; int pz;
ll D[1000010];
ll d2[1000010], temp[1000010];

void Do(vector <ll> &X, vector <ll> &Y) {
	int zx = szz(X), zy = szz(Y);
	d2[0] = D[0];
	for(int i=1;i<=zx;i++) d2[i] = min(d2[i-1] + Y[0] - X[i-1], D[i]);
	temp[0] = D[zx];
	ll sx = 0, sy = 0;
	for(int i=1;i<=zy;i++) {
		temp[i] = temp[i-1] + Y[i-1] - X.back();
		if(i <= zx) {
			sy += Y[i-1];
			sx += X[zx - i];
			temp[i] = min(temp[i], d2[zx - i] + sy - sx);
		}
	}
	for(int i=0;i<=zy;i++) D[i] = temp[i];
}


long long min_total_length(std::vector<int> r, std::vector<int> b) {
	n = szz(r), m = szz(b);
	for(int i=1;i<=n;i++) S[i] = r[i-1];
	for(int i=1;i<=m;i++) T[i] = b[i-1];
	for(int i=1, j=1;i<=n || j<=m;) {
		if(i > n || (j <= m && T[j] < S[i])) p[++pz] = pii(T[j++], 1);
		else p[++pz] = pii(S[i++], 0);
	}
	int f = -1;
	for(int i=2;i<=pz;i++) if(p[i].Se != p[i-1].Se) { f = i; break; }
	vector <ll> A, B;
	for(int i=1;i<f;i++) A.pb(p[i].Fi);
	for(int i=1;i<=szz(A);i++) D[i] = 1e18;
	for(int i=f;i<=pz;) {
		int j = i;
		while(j <= pz && p[i].Se == p[j].Se) B.pb(p[j++].Fi);
		Do(A, B);
		i = j;
		swap(A, B); B.clear();
	}
	return D[szz(A)];
}
#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...