제출 #1181630

#제출 시각아이디문제언어결과실행 시간메모리
1181630HappyCapybara전선 연결 (IOI17_wiring)C++17
0 / 100
1095 ms5816 KiB
#include "wiring.h"
#include<bits/stdc++.h>
using namespace std;

#define ll long long

ll min_total_length(vector<int> r, vector<int> b){
	int n = r.size();
	int m = b.size();
	vector<pair<int,int>> v;
	for (int i=0; i<n; i++) v.push_back({r[i], 0});
	for (int i=0; i<m; i++) v.push_back({b[i], 1});
	sort(v.begin(), v.end());
	vector<int> bl(n+m);
	bl[0] = 1;
	for (int i=1; i<n+m; i++){
		if (v[i].second == v[i-1].second) bl[i] = bl[i-1]+1;
		else bl[i] = 1;
	}
	vector<ll> dp(n+m+1, 1ll<<50);
	dp[0] = 0;
	for (int i=0; i<n+m; i++){
		if (bl[i] == i+1) continue;
		int lb = i-bl[i];
		for (int j=0; j<bl[lb]; j++){
			ll k = 0;
			for (int x=0; x<min(bl[i], j+1); x++) k += abs(v[lb-x].first-v[lb+1+x].first);
			for (int x=bl[i]; x<j+1; x++) k += abs(v[lb-x].first-v[lb+1].first);
			for (int x=j+1; x<bl[i]; x++) k += abs(v[lb].first-v[lb+1+x].first);
			//cout << i << " " << j << " " << k << endl;
			if (j != 0 && j != bl[i]-1 && j != bl[i] && j != bl[lb]-1) continue;
			if (j == bl[lb]-1) dp[i+1] = min(dp[i+1], k+min(dp[lb-j], dp[lb-j+1]));
			else dp[i+1] = min(dp[i+1], k+dp[lb-j]);
		}
	}
	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...