Submission #1320191

#TimeUsernameProblemLanguageResultExecution timeMemory
1320191kasamchiWiring (IOI17_wiring)C++20
0 / 100
0 ms332 KiB
#include "wiring.h"
#include <algorithm>
#include <vector>
using namespace std;

long long min_total_length(vector<int> r, vector<int> b) {
	int N = r.size() + b.size();
	vector<pair<int, int>> v;
	for (int i = 0; i < r.size(); i++) {
		v.push_back(make_pair(r[i], 0));
	}
	for (int i = 0; i < b.size(); i++) {
		v.push_back(make_pair(b[i], 1));
	}
	sort(v.begin(), v.end());

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