#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 (false)///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
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 |
1 |
Correct |
1 ms |
2392 KB |
n = 2 |
2 |
Correct |
1 ms |
2396 KB |
n = 2 |
3 |
Correct |
0 ms |
2396 KB |
n = 2 |
4 |
Correct |
1 ms |
2392 KB |
n = 2 |
5 |
Correct |
1 ms |
2396 KB |
n = 2 |
6 |
Incorrect |
1 ms |
2396 KB |
answer is not correct: 1 instead of 523688153 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
n = 2 |
2 |
Correct |
1 ms |
2396 KB |
n = 2 |
3 |
Correct |
0 ms |
2396 KB |
n = 2 |
4 |
Correct |
1 ms |
2392 KB |
n = 2 |
5 |
Correct |
1 ms |
2396 KB |
n = 2 |
6 |
Incorrect |
1 ms |
2396 KB |
answer is not correct: 1 instead of 523688153 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
122 ms |
18796 KB |
n = 199999 |
2 |
Correct |
94 ms |
18628 KB |
n = 199991 |
3 |
Correct |
94 ms |
18624 KB |
n = 199993 |
4 |
Correct |
89 ms |
13184 KB |
n = 152076 |
5 |
Correct |
54 ms |
9168 KB |
n = 93249 |
6 |
Correct |
111 ms |
18808 KB |
n = 199910 |
7 |
Correct |
111 ms |
21952 KB |
n = 199999 |
8 |
Correct |
109 ms |
21440 KB |
n = 199997 |
9 |
Correct |
108 ms |
17880 KB |
n = 171294 |
10 |
Correct |
83 ms |
15040 KB |
n = 140872 |
11 |
Correct |
87 ms |
21444 KB |
n = 199886 |
12 |
Correct |
100 ms |
21824 KB |
n = 199996 |
13 |
Correct |
86 ms |
21396 KB |
n = 200000 |
14 |
Correct |
83 ms |
21696 KB |
n = 199998 |
15 |
Correct |
84 ms |
21700 KB |
n = 200000 |
16 |
Correct |
84 ms |
21960 KB |
n = 199998 |
17 |
Correct |
93 ms |
22716 KB |
n = 200000 |
18 |
Correct |
82 ms |
21540 KB |
n = 190000 |
19 |
Correct |
88 ms |
18440 KB |
n = 177777 |
20 |
Correct |
53 ms |
11716 KB |
n = 100000 |
21 |
Correct |
119 ms |
22524 KB |
n = 200000 |
22 |
Correct |
97 ms |
22460 KB |
n = 200000 |
23 |
Correct |
130 ms |
22476 KB |
n = 200000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
n = 2 |
2 |
Correct |
1 ms |
2396 KB |
n = 2 |
3 |
Correct |
0 ms |
2396 KB |
n = 2 |
4 |
Correct |
1 ms |
2392 KB |
n = 2 |
5 |
Correct |
1 ms |
2396 KB |
n = 2 |
6 |
Incorrect |
1 ms |
2396 KB |
answer is not correct: 1 instead of 523688153 |
7 |
Halted |
0 ms |
0 KB |
- |