제출 #739861

#제출 시각아이디문제언어결과실행 시간메모리
739861sentheta전선 연결 (IOI17_wiring)C++17
17 / 100
1087 ms10296 KiB
#include "wiring.h"
#include<vector>
#include<iostream>
#include<algorithm>
#include<utility>
using namespace std;
#define Int long long
#define rep(i,a,b) for(Int i=a; i<(Int)b; i++)
#define V vector
#define nl '\n'
#define _ << " " << 
#define dbg(x) cout << "?" << #x << " : " << x << endl;

const Int INF = 1e18;
const Int N = 2e5+5;

Int n, m;
V<Int> a, t;

Int prf[N];
Int rgsum(Int l,Int r){
	return prf[r] - prf[l-1];
}

Int dp[N];

Int min_total_length(V<int> red,V<int> blue){
	n = red.size(); m = blue.size();

	a = t = {-1};
	{
		Int i=0, j=0;
		while(i<n && j<m){
			if(red[i] < blue[j]){
				a.push_back(red[i++]); t.push_back(0);
			}
			else{
				a.push_back(blue[j++]); t.push_back(1);
			}
		}
		while(i<n){
			a.push_back(red[i++]); t.push_back(0);
		}
		while(j<m){
			a.push_back(blue[j++]); t.push_back(1);
		}
	}
	// cout << "a[] ";
	rep(i,1,n+m+1){
		prf[i] = prf[i-1] + a[i];
		// cout << a[i] << " ";
	}
	// cout << nl;

	dp[0] = 0;
	rep(i,1,n+m+1){
		// dbg(a[i]);
		dp[i] = INF;

		Int k = i;
		while(t[k]==t[i]) k--;
		if(k==0) continue;

		for(Int j=k; t[j]==t[k]; j--){
			Int lef=k-j+1, rig=i-(k+1)+1;

			dp[i] = min({
				dp[i],
				min(dp[j-1],dp[j])
				+ lef*a[k] - rgsum(j,k)
				+ max(lef,rig)*(a[k+1]-a[k])
				+ rgsum(k+1,i) - rig*a[k+1]
			});
		}

		// dbg(dp[i]);
		// cout << nl;
	}

	Int ans = dp[n+m];
	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...