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<bits/stdc++.h>
#include "railroad.h"
using namespace std;
#define sz(v) ((int)(v).size())
typedef long long lint;
typedef pair<int,int> pii;
int par[200002];
int find(int p)
{
if(par[p]==p)return p;
else return par[p]=find(par[p]);
}
void uni(int p,int q)
{
p=find(p);
q=find(q);
if(p!=q)par[p]=q;
}
int v[200002];
int line[200002],sT[200002],num[200002],ori[200002],col;
void dfs(int p)
{
v[p]=col;
if(!v[sT[p]])dfs(sT[p]);
}
lint plan_roller_coaster(vector<int> s, vector<int> t) {
int n = (int) s.size();
lint ans=0;
vector<pii>S;
vector<pii>T;
for(int i=0;i<n;i++)S.push_back({s[i],i});
for(int i=0;i<n;i++)T.push_back({t[i],i});
S.push_back({1e9+1,n});T.push_back({0,n});
sort(S.begin(),S.end());
sort(T.begin(),T.end());
for(int i=0;i<=n;i++)num[T[i].second]=i;
for(int i=0;i<=n;i++)
{
ori[i]=T[i].first;
if(T[i].first>S[i].first)
{
ans+=T[i].first-S[i].first;
T[i].first=S[i].first;
}
}
int j=0;
for(int i=0;i<=n;i++)
{
while(j<=n && T[i].first>S[j].first)j++;
line[i]=j;
sT[i]=num[S[i].second];
}
col=0;
for(int i=0;i<=n;i++)
{
if(!v[i])
{
col++;
dfs(i);
}
}
int mini,maxi;
priority_queue<pii>P;
for(int i=1;i<=col;i++)par[i]=i;
for(int i=0;i<=n;)
{
int k=i;
mini=ori[i];
maxi=T[i].first;
while(k+1<=n)
{
mini=min(mini,ori[k+1]);
maxi=max(maxi,T[k+1].first);
if(mini>=maxi)k++;
else break;
}
for(int l=i+1;l<=k;l++)uni(v[l],v[l-1]);
i=k+1;
}
for(int i=0;i<=n;i++)
{
if(line[i]!=i && find(v[i-1])!=find(v[i]))uni(v[i-1],v[i]);
if(line[i]==i && i>0)P.push({-(T[i].first-max(ori[i-1],S[i-1].first)),i});
}
while(!P.empty())
{
pii p=P.top();
P.pop();
if(find(v[p.second])!=find(v[p.second-1]))
{
uni(v[p.second-1],v[p.second]);
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... |