제출 #153251

#제출 시각아이디문제언어결과실행 시간메모리
153251qkxwsmRoller Coaster Railroad (IOI16_railroad)C++14
64 / 100
308 ms19952 KiB
#include "railroad.h"
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
	if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
	if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define MAXN 400013
#define INF 1000000007
#define LLINF 2696969696969696969

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N;
pll arr[MAXN];
ll dp[(1 << 17)][17];
ll ans = LLINF;
int pref[MAXN];
vl guys;
int dsu[MAXN];
int cc;

int get(int u)
{
	return (u == dsu[u] ? u : dsu[u] = get(dsu[u]));
}
bool merge(int u, int v)
{
	u = get(u); v = get(v);
	if (u == v) return false;
	dsu[u] = v;
	return true;
}

ll plan_roller_coaster(vector<int> s, vector<int> t)
{
	N = SZ(s);
	FOR(i, 0, N)
	{
		arr[i] = {s[i], t[i]};
		arr[i].fi--; arr[i].se--;
	}
	if (N <= 17)
	{
		for (int i = 0; i < (1 << N); i++)
		{
			for (int j = 0; j < N; j++)
			{
				dp[i][j] = LLINF;
			}
		}
		for (int i = 0; i < N; i++)
		{
			dp[(1 << i)][i] = 0;
		}
		for (int i = 0; i < (1 << N); i++)
		{
			for (int j = 0; j < N; j++)
			{
				if (!(i & (1 << j)))
				{
					continue;
				}
				for (int k = 0; k < N; k++)
				{
					if (i & (1 << k))
					{
						continue;
					}
					dp[i + (1 << k)][k] = min(dp[i + (1 << k)][k], dp[i][j] + max(0ll, arr[j].se - arr[k].fi));
				}
			}
		}
		for (int i = 0; i < N; i++)
		{
			ckmin(ans, dp[(1 << N) - 1][i]);
		}
		return ans;
	}
	arr[N] = {INF, 0};
	N++;
	FOR(i, 0, N)
	{
		guys.PB(arr[i].fi);
		guys.PB(arr[i].se);
	}
	sort(ALL(guys));
	guys.erase(unique(ALL(guys)), guys.end());
	cc = SZ(guys);
	FOR(i, 0, SZ(guys))
	{
		dsu[i] = i;
	}
	FOR(i, 0, N)
	{
		int lt = arr[i].fi, rt = arr[i].se;
		lt = LB(ALL(guys), lt) - guys.begin();
		rt = LB(ALL(guys), rt) - guys.begin();
		pref[lt]++;
		pref[rt]--;
	}
	FOR(i, 1, SZ(guys))
	{
		pref[i] += pref[i - 1];
	}
	FOR(i, 0, SZ(guys) - 1)
	{
		if (pref[i] > 0)
		{
			return 69;
		}
		if (pref[i] < 0)
		{
			if (merge(i, i + 1)) cc--;
		}
	}
	FOR(i, 0, N)
	{
		int lt = arr[i].fi, rt = arr[i].se;
		lt = LB(ALL(guys), lt) - guys.begin();
		rt = LB(ALL(guys), rt) - guys.begin();
		if (merge(lt, rt)) cc--;
	}
	return (cc == 1 ? 0 : 69);
	//you cannot insert -1s. you can get +1s but it means accelerate.
	//ok mark the guys that are
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...