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 ll long long
#define pb push_back
#define bit(a , b) (((a)>>(b))&1)
const int maxn = 2e5 + 20;
const int maxm = (1 << 16) + 20;
int ind[maxn] , s[maxn] , t[maxn] , n;
ll dp[maxm][20];
ll solve()
{
memset(dp , 63 , sizeof dp);
int hlp = (1 << n);
for(int mask = 1; mask < hlp; mask++)
for(int i = 0; i < n; i++)
if(bit(mask , i))
{
if(mask == (1 << i))
{
dp[mask][i] = 0;
break;
}
for(int j = 0; j < n; j++)
if(bit(mask ^ (1 << i) , j))
dp[mask][i] = min(dp[mask][i] , dp[mask ^ (1 << i)][j] + max(0 , t[j] - s[i]));
}
ll ans = *min_element(dp[hlp - 1] , dp[hlp - 1] + n);
return ans;
}
bool cmp(int x , int y)
{
return t[x] < t[y];
}
long long plan_roller_coaster(vector<int> S, vector<int> T)
{
n = (int)S.size();
for(int i = 0; i < n; i++)
s[i] = S[i] , t[i] = T[i] , ind[i] = i;
if(n <= 16)
return solve();
sort(ind , ind + n , cmp);
set<pair<int , int> > st , shit;
for(int i = 0; i < n; i++)
{
s[i] = S[ind[i]];
t[i] = T[ind[i]];
st.insert({s[i] , i});
}
for(int i = n - 1; i >= 0; i--)
{
if(shit.lower_bound(make_pair(t[i] , -1e9)) != shit.end())
{
shit.erase(shit.lower_bound(make_pair(t[i] , -1e9)));
continue;
}
st.erase({s[i] , i});
auto it = st.lower_bound(make_pair(t[i] , -1e9));
if(it == st.end())
{
shit.insert({s[i] , i});
continue;
}
int x = it -> second;
st.erase(it);
s[x] = s[i];
st.insert({s[x] , x});
}
return ((int)st.size() + (int)shit.size() <= 1? 0 : 1);
}
# | 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... |