이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
lint D[1<<16][16];
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 real[200002],mini,maxi;
priority_queue<pii>P;
for(int i=1;i<=col;i++)real[i]=i;
for(int i=0;i<=n;)
{
int k=i;
int minreal=col+1;
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;l<=k;l++)
{
minreal=min(minreal,real[v[l]]);
}
for(int l=i;l<=k;l++)real[v[l]]=minreal;
i=k+1;
}
for(int i=0;i<=n;i++)
{
if(line[i]!=i && real[v[i-1]]!=real[v[i]])
{
real[v[i]]=real[v[i-1]]=min(real[v[i]],real[v[i-1]]);
}
if(line[i]==i && i>0)
{
P.push({-(T[i].first-S[i-1].first),i});
}
}
while(!P.empty())
{
pii p=P.top();
P.pop();
if(real[v[p.second]]!=real[v[p.second-1]])
{
real[v[p.second]]=real[v[p.second-1]]=min(real[v[p.second]],real[v[p.second-1]]);
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... |