제출 #406714

#제출 시각아이디문제언어결과실행 시간메모리
406714Kevin_Zhang_TWWiring (IOI17_wiring)C++17
100 / 100
82 ms8208 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif

const int MAX_N = 300010;
const ll inf = 1ll << 55;

#include "wiring.h"

long long min_total_length(std::vector<int> r, std::vector<int> b) {

	priority_queue<ll, vector<ll>, greater<ll>> pq[2];

	vector<pair<int,int>> all;
	for (int x : r) all.pb(x, 0);
	for (int x : b) all.pb(x, 1);
	sort(AI(all));

	ll res = 0, mx[2]{-inf, -inf};

	for (auto [x, c] : all) {
		int cnt = 0;
		ll ma = -mx[c^1];

		while (pq[c^1].size() && pq[c^1].top() + x <= 0) {
			ll tp = pq[c^1].top(); pq[c^1].pop();
			res += x + tp;
			++cnt;
		}
		if (cnt == 0 && pq[c^1].size() && pq[c^1].top() < ma) {
			ma = pq[c^1].top(); pq[c^1].pop();
		}

		if (cnt == 0) {
			ll cost = x + ma;
			res += cost;
			pq[c].emplace(-cost-x);
		}

		mx[c] = x;

		for (int i = 1;i < cnt;++i) {
			pq[c^1].emplace(-x);
		}

	}

	return res;
}
#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...