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;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
#define debug(x) {cerr<<#x<<"="<<x<<"\n";}
#define debug2(x, y) {cerr<<#x<<", "<<#y<<" = "<<x<<", "<<y<<"\n";}
#define pb push_back
#define all(x) x.begin(), x.end()
const int inf=1000000010;
const ll INF=10000001000000000ll;
const int MAXN=400010;
int n, m, k;
int S[MAXN], T[MAXN], ps[MAXN];
int par[MAXN];
bool mark[MAXN];
// ll dp[1<<16][16];
vector<int> comp;
ll ans;
int getpar(int x){ return (par[x]==x?x:par[x]=getpar(par[x]));}
ll plan_roller_coaster(vector<int> s, vector<int> t){
n=s.size();/*
if (n<=16){
for (int mask=1; mask<(1<<n); mask++){
int v=__builtin_ctz(mask);
if (mask==(1<<v)) dp[mask][v]=0;
else{
for (int v=0; v<n; v++) if (mask&(1<<v)){
dp[mask][v]=INF;
for (int u=0; u<n; u++) if (u!=v && mask&(1<<u))
dp[mask][v]=min(dp[mask][v], dp[mask^(1<<v)][u] + max(0, t[u]-s[v]));
}
}
}
ll ans=INF;
for (int i=0; i<n; i++) ans=min(ans, dp[(1<<n)-1][i]);
return ans;
}*/
for (int i=1; i<=n; i++) S[i]=s[i-1], T[i]=t[i-1];
S[0]=inf;
T[0]=1;
for (int i=0; i<=n; i++){
comp.pb(T[i]);
comp.pb(S[i]);
}
sort(all(comp));
comp.resize(unique(all(comp))-comp.begin());
m=comp.size();
iota(par, par+m, 0);
for (int i=0; i<=n; i++){
S[i]=lower_bound(all(comp), S[i])-comp.begin();
T[i]=lower_bound(all(comp), T[i])-comp.begin();
par[getpar(S[i])]=getpar(T[i]);
// debug2(S[i], T[i])
ps[S[i]]++;
ps[T[i]]--;
}
for (int i=1; i<m; i++) ps[i]+=ps[i-1];
for (int i=0; i+1<m; i++) if (ps[i]){
// debug2(i, comp[i])
par[getpar(i)]=getpar(i+1);
if (ps[i]>0) ans+=1ll*ps[i]*(comp[i+1]-comp[i]);
}
// debug(ans)
vector<pair<ll, pii>> E;
for (int i=0; i<=n; i++) mark[getpar(S[i])]=1;
int last=-1;
for (int i=0; i<m; i++) if (mark[getpar(i)]){
if (last!=-1) E.pb({comp[i]-comp[last], {i, last}});
last=i;
}
sort(all(E));
for (auto p:E){
int x=getpar(p.second.first), y=getpar(p.second.second);
if (x!=y){
par[x]=y;
ans+=p.first;
}
}
return ans;
}
# | 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... |