제출 #1358853

#제출 시각아이디문제언어결과실행 시간메모리
1358853vidux전선 연결 (IOI17_wiring)C++17
100 / 100
34 ms9808 KiB
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;

long long min_total_length(std::vector<int> r, std::vector<int> b) {
	const int nm = r.size()+b.size();
	vector<pair<ll, bool>> p;
	for (ll x : r) p.push_back({x, 0});
	for (ll x : b) p.push_back({x, 1});
	sort(p.begin(), p.end());
	vector<ll> ord[2];
	for (auto [x, c] : p) ord[c].push_back(x);
	vl dp(nm+1, 1e18);
	dp[0] = 0;
	ll pr = -1, cs = 0;
	for (int i = 0; i < nm; i++) {
		auto [x, c] = p[i];
		ll add = 1e18;
		ll lb = lower_bound(ord[!c].begin(), ord[!c].end(), x)-ord[!c].begin();
		if (lb < ord[!c].size()) add = min(add, ord[!c][lb]-x);
		if (lb-1 >= 0) add = min(add, x-ord[!c][lb-1]);
		dp[i+1] = dp[i]+add;
		if (i && p[i].second != p[i-1].second) {
			pr = i-1;
			cs = p[i].first-p[i-1].first;
		}
		else if (pr > 0 && p[pr].second == p[pr-1].second) {
			pr--;
			cs += p[i].first-p[pr].first;
		}
		else pr = -1;
		if (pr >= 0) dp[i+1] = min(dp[i+1], dp[pr]+cs);
	}
	return dp[nm];
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…