제출 #290751

#제출 시각아이디문제언어결과실행 시간메모리
290751SaboonWiring (IOI17_wiring)C++17
7 / 100
1097 ms5100 KiB
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
ll dp[220][220], opt[220][220];
int n, m;
ll a[maxn], b[maxn];
ll par[maxn];

ll Cost(int l, int r, int i){
	int idx = lower_bound(b+l, b+r+1, a[i])-b;
	ll ret = 0;
	if (idx > l)
		ret += (idx-l)*a[i] - (par[idx-1]-par[l-1]);
	if (idx <= r)
		ret += (par[r]-par[idx-1]) - (r-idx+1)*a[i];
	return ret;
}

ll min_total_length(vector<int> Red, vector<int> Blue){
	n = Red.size(), m = Blue.size();
	for (int i = 1; i <= n; i++)
		a[i] = Red[i-1];
	for (int i = 1; i <= m; i++)
		b[i] = Blue[i-1];
	for (int i = 1; i <= m; i++)
		par[i] = par[i-1]+b[i];
	memset(dp, 63, sizeof dp);
	dp[0][0] = 0;
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= m; j++){
			dp[i][j] = dp[i-1][j] + abs(a[i]-b[j]);
			opt[i][j] = j;
			for (int k = 1; k <= j; k++){
				ll now = dp[i-1][k-1] + Cost(k,j,i);
				if (now <= dp[i][j])
					dp[i][j] = now, opt[i][j] = k-1;
			}
		}
		for (int j = 1; j < m; j++)
			assert(opt[i][j] <= opt[i][j+1]);
	}
	return dp[n][m];
}
#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...