제출 #1320209

#제출 시각아이디문제언어결과실행 시간메모리
1320209kasamchi전선 연결 (IOI17_wiring)C++20
0 / 100
0 ms332 KiB
#include "wiring.h"
#include <iostream>
#include <vector>
using namespace std;

long long min_total_length(vector<int> r, vector<int> b) {
	int N = r.size() + b.size();
	int ridx = 0, bidx = 0;
	vector<pair<int, int>> v;
	while (ridx < r.size() || bidx < b.size()) {
		if (bidx == b.size()) {
			v.push_back(make_pair(r[ridx], 0));
			ridx++;
		} else if (ridx == r.size()) {
			v.push_back(make_pair(b[bidx], 1));
			bidx++;
		} else if (r[ridx] < b[bidx]) {
			v.push_back(make_pair(r[ridx], 0));
			ridx++;
		} else {
			v.push_back(make_pair(b[bidx], 1));
			bidx++;
		}
	}

	vector<long long> dp(N, (long long)1e18);
	for (int idx = 0; idx < N; ) {
		int clb = idx, crb = idx;
		while (crb < N && v[crb].second == v[clb].second) {
			crb++;
		}
		idx = crb;
		crb--;
		if (clb == 0) {
			continue;
		}

		int prb = clb - 1, plb = prb;
		while (plb >= 0 && v[plb].second == v[prb].second) {
			plb--;
		}
		plb++;

		for (int c = clb; c <= crb; c++) {
			for (int p = plb; p <= (plb == 0 ? plb : prb); p++) {
				int i = p, j = clb;
				long long cur = (plb == 0 ? 0 : min(dp[p - 1], dp[p]));
				while (i <= prb && j <= c) {
					cur += v[j].first - v[i].first;
					i++, j++;
				}
				while (i <= prb) {
					cur += v[clb].first - v[i].first;
					i++;
				}
				while (j <= c) {
					cur += v[j].first - v[prb].first;
					j++;
				}
				dp[c] = min(dp[c], cur);
			}
		}
	}

	for (int i = 0; i < N; i++) {
		cout << dp[i] << " \n"[i == N - 1];
	}
	return dp[N - 1];
}
#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...