이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
const int inf=1e9+7;
struct Event
{
int x,t,id;
Event(int a, int b, int c){ x=a,t=b,id=c;}
bool operator < (Event b) const { return x<b.x || (x==b.x && t<b.t);}
};
struct Edge
{
int u,v,w;
Edge(int a, int b, int c){ u=a,v=b,w=c;}
bool operator < (Edge b) const { return w<b.w;}
};
const int N=200050;
int p[N];
void init(){ for(int i=0;i<N;i++) p[i]=i;}
int Find(int x){ return p[x]==x?x:p[x]=Find(p[x]);}
void Union(int x, int y){ p[Find(x)]=Find(y);}
int max(int a, int b){ return a>b?a:b;}
ll plan_roller_coaster(vector<int> s, vector<int> t)
{
int n=s.size(),i;
vector<Event> events;
for(i=0;i<n;i++) events.pb(Event(s[i],1,i+1)),events.pb(Event(t[i],-1,i+1));
events.pb(Event(inf,1,n+1));
events.pb(Event(1,-1,n+1));
int balance=0;
ll sol=0;
init();
sort(events.begin(),events.end());
vector<Edge> edges;
for(i=0;i<events.size()-1;i++)
{
int len=events[i+1].x-events[i].x;
balance+=events[i].t;
//printf("%i %i %i %i %i\n",events[i].x,len,balance,events[i].id,events[i+1].id);
sol+=(ll)max(0,balance)*len;
if(balance==0 && len>0) edges.pb(Edge(events[i].id,events[i+1].id,len));
else Union(events[i].id,events[i+1].id);
}
sort(edges.begin(),edges.end());
for(i=0;i<edges.size();i++)
{
//printf("%i %i %i %i %i\n",edges[i].u,edges[i].v,edges[i].w,Find(edges[i].u),Find(edges[i].v));
if(Find(edges[i].u)!=Find(edges[i].v))
{
sol+=edges[i].w;
Union(edges[i].u,edges[i].v);
}
}
return sol;
}
/*int main()
{
int n,i;
vector<int> s,t;
scanf("%i",&n);
s.resize(n);t.resize(n);
for(i=0;i<n;i++) scanf("%i %i",&s[i],&t[i]);
printf("%lld\n",plan_roller_coaster(s,t));
return 0;
}*/
컴파일 시 표준 에러 (stderr) 메시지
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:39:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<events.size()-1;i++)
~^~~~~~~~~~~~~~~~
railroad.cpp:49:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<edges.size();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... |