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;
typedef long long ll;
const ll inf = 1e18;
const int maxn = 2e5 + 10;
ll dp[17][(1 << 17)];
pair < int, int > sec[maxn];
int sp[maxn];
int find_leader(int v)
{
if (v == sp[v])
return v;
return (sp[v] = find_leader(sp[v]));
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t)
{
int n = (int) s.size();
if (n <= 17)
{
for (int i = 0; i < n; i ++)
for (int mask = 0; mask < (1 << n); mask ++)
dp[i][mask] = inf;
for (int i = 0; i < n; i ++)
dp[i][(1 << i)] = 0;
for (ll mask = 0; mask < (1 << n); mask ++)
{
for (int i = 0; i < n; i ++)
{
if ((mask & (1 << i)) == 0)
continue;
//cout << "state " << i << " " << mask << " " << dp[i][mask] << endl;
for (int j = 0; j < n; j ++)
{
if ((mask & (1 << j)) != 0)
continue;
int new_mask = (mask | (1 << j));
//cout << "transition " << j << " " << max(0, t[i] - s[j]) << " " << t[i] << " " << s[j] << endl;
dp[j][new_mask] = min(dp[j][new_mask], dp[i][mask] + (ll)max(0, t[i] - s[j]));
}
}
}
ll ans = inf;
for (int i = 0; i < n; i ++)
ans = min(ans, dp[i][(1 << n) - 1]);
return ans;
}
else
{
for (int i = 0; i < n; i ++)
{
///assert(t[i] != 0 && s[i] != 0);
sec[i + 1] = {t[i], s[i]};
}
sort(sec + 1, sec + n + 1);
vector < pair < int, int > > ord;
for (int i = 1; i <= n; i ++)
{
//assert(sec[i].first != 0 && sec[i].second != 0);
ord.push_back({sec[i].second, i});
}
sort(ord.begin(), ord.end());
set < int > act;
for (int i = 1; i <= n; i ++)
act.insert(i);
for (int i = 1; i <= n; i ++)
sp[i] = i;
bool start = false;
int cnt = 0;
for (pair < int, int > cur : ord)
{
//cnt ++;
//if (cnt % 10000 == 0 || cnt > 91000)
//cout << cnt << " " << cur.second << " " << cur.first << " " << endl;
int lf = 1, rf = n;
while(lf <= rf)
{
int mf = (lf + rf) / 2;
if (sec[mf].first <= cur.first)
lf = mf + 1;
else
rf = mf - 1;
}
set < int > :: iterator it = act.lower_bound(lf);
if (it == act.begin())
{
if (start == true)
return 1;
start = true;
}
else
{
it = prev(it);
//if (cnt % 10000 == 0 || cnt > 91000)
///cout << "fine " << " " << *it << " " << find_leader(sp[cur.second]) << endl;
if (*it == find_leader(sp[cur.second]))
{
if (it == act.begin())
{
if (start == true)
return 1;
///cout << "fuck" << endl;
start = true;
continue;
}
else
{
it = prev(it);
}
}
sp[*it] = sp[cur.second];
act.erase(it);
}
}
return 0;
}
}
Compilation message (stderr)
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:85:13: warning: unused variable 'cnt' [-Wunused-variable]
85 | int cnt = 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... |