This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "railroad.h"
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define f first
#define s second
#define pb push_back
typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<pii> vii;
const int MAXN = 1000100;
const ll INF = 1e16;
int bitCnt (int x, int n) {
int ret = 0;
FOR(i, 0, n-1)
if (x & (1<<i))
ret ++;
return ret;
}
ll dp[1 << 17][20];
ll solveDP (vi s, vi t, int n) {
//FOR(i, 0, n-1) {
// cout << " i = " << i << " s = " << s[i] << " t = " << t[i] << endl;
//}
vii srt;
FOR(mask, 0, (1<<n)-1) {
if (mask != 0) srt.pb({bitCnt(mask, n), mask});
FOR(i, 0, n-1) dp[mask][i] = INF;
}
sort(srt.begin(), srt.end());
//FOR(i, 0, n-1) dp[0][i] = 0;
for (auto p : srt) {
int mask = p.s;
int bcnt = p.f;
if (bcnt == 1) {
int b = 0;
FOR(i, 0, n-1)
if (mask & (1<<i))
b = i;
dp[mask][b] = 0ll;
} else {
FOR(i, 0, n-1) FOR(j, 0, n-1) {
if (!(mask & (1<<i))) continue;
if (!(mask & (1<<j))) continue;
ll newVal = dp[mask ^ (1<<i)][j];
newVal += max(0ll, (ll)t[j] - s[i]);
dp[mask][i] = min(dp[mask][i], newVal);
}
}
}
ll ret = dp[(1<<n)-1][0];
FOR(i, 0, n-1) ret = min (ret, dp[(1<<n)-1][i]);
return ret;
}
int pref[MAXN];
int par[MAXN];
int balance[MAXN];
int find (int a) {return par[a] = par[a]==a ? a : find(par[a]);}
bool same (int a, int b) {return find(a) == find(b);}
void unite (int a, int b) {
a = find(a), b = find(b);
if (a == b) return;
par[b] = a;
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
int n = (int) s.size();
if (n <= 16) return solveDP(s, t, n);
s.pb((int)1000 * 1000 * 1000);
t.pb(1);
set<int> cur;
map<int, int> toId;
FOR(i, 0, n) cur.insert(s[i]);
FOR(i, 0, n) cur.insert(t[i]);
int curId = 0;
for (int x : cur)
toId[x] = ++curId;
FOR(i, 0, curId+5) par[i] = i;
FOR(i, 0, n) s[i] = toId[s[i]];
FOR(i, 0, n) t[i] = toId[t[i]];
//FOR(i, 0, n) cout << " i = " << i << " s[i] = " << s[i] << " t[i] = " << t[i] << endl;
FOR(i, 0, n) pref[s[i]]++, pref[t[i]]--;
// FOR(i, 1, curId) cout <<" i = " << i<< " pref: " << pref[i] << endl;
int ss = 0;
FOR(i, 1, curId-1) {
ss += pref[i];
balance[i] = ss;
// cout << " i = " << i << " balance: " << balance[i] << endl;
}
FOR(i, 0, n) unite(s[i], t[i]);
FOR(i, 1, curId-1) {
if (balance[i] > 0) return 1;
if (balance[i] < 0) unite(i, i+1);
}
FOR(i, 1, curId-1)
if (!same(i, i+1))
return 1;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |