이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define db long double
#define II pair <ll,ll>
#define III pair <ll,II>
#define IV pair <vector <int>,vector <int> >
#define IDB pair <db,int>
#define TII pair <treap*,treap*>
#define fst first
#define snd second
#define BIT(x,i) ((x>>i)&1)
#define pi acos(-1)
#define to_radian(x) (x*pi/180.0)
#define to_degree(x) (x*180.0/pi)
#define Log(x) (31-__builtin_clz((int)x))
#define LogLL(x) (63-__builtin_clzll((ll)x))
using namespace std;
#include "railroad.h"
const int oo=2e9;
ll Real[200005],r[200005],u,U[200005],D[200005];
int root(int u) { return u==r[u] ? u : r[u]=root(r[u]); }
struct edge { int u,v,w; } e[200005];
bool join(int u,int v)
{
u=root(u); v=root(v);
if(u==v) return 0;
r[u]=v;
return 1;
}
ll plan_roller_coaster(vector <int> s,vector <int> t)
{
set <int> val;
map <int,int> rr;
val.insert(1);
val.insert(oo);
for(int x:s) val.insert(x);
for(int x:t) val.insert(x);
int n_val=0;
for(int x:val) rr[x]=++n_val,Real[n_val]=x;
int n=s.size(),i,up=0,down=0;
ll res=0;
for(u=1;u<=n_val;u++) r[u]=u;
D[1]++; D[n_val]--;
join(1,n_val);
for(i=0;i<n;i++)
{
s[i]=rr[s[i]]; t[i]=rr[t[i]];
if(s[i]<=t[i]) U[s[i]]++,U[t[i]]--;
else D[t[i]]++,D[s[i]]--;
join(s[i],t[i]);
}
for(i=1;i<n_val;i++)
{
up+=U[i]; down+=D[i];
res+=max(0,up-down)*(Real[i+1]-Real[i]);
if(up!=down) join(i,i+1);
e[i]={ i,i+1,Real[i+1]-Real[i] };
}
sort(e+1,e+n_val,[&](edge a,edge b){ return a.w<b.w; });
for(i=1;i<n_val;i++)
if(join(e[i].u,e[i].v)) res+=e[i].w;
return res;
}
/*
int main()
{
freopen("railroad.inp","r",stdin);
freopen("railroad.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
vector <int> s,t;
int l,r;
while(cin>>l>>r) s.push_back(l),t.push_back(r);
cout<<plan_roller_coaster(s,t);
}
*/
컴파일 시 표준 에러 (stderr) 메시지
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:60:31: warning: narrowing conversion of '(Real[(i + 1)] - Real[i])' from 'long long int' to 'int' [-Wnarrowing]
60 | e[i]={ i,i+1,Real[i+1]-Real[i] };
| ~~~~~~~~~^~~~~~~~
# | 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... |