이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "railroad.h"
#define f first
#define s second
#define vec vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pw(x) (1LL<<(x))
#define sz(x) (int)(x).size()
#define m_p make_pair
#define fast_ioi ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
const int N=4e5+4;
int p[N];
void make(int v){
p[v]=v;
}
int get(int v){
return (p[v]==v?v:get(p[v]));
}
int mg(int a,int b){
a=get(a),b=get(b);
if(a==b) return 0;
p[a]=b;
return 1;
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t){
vec<int> x;
s.pb(1e9);t.pb(1);
map<int,int> mp;
map<int,int> cons;
map<int,int> id;
int tt=0;
for(int i=0;i<sz(s);i++){
if(s[i]<t[i]) mp[s[i]]++,mp[t[i]]--,cons[s[i]]++,cons[t[i]]--;
else mp[t[i]]--,mp[s[i]]++;
}
int bl=0,jt=0;
vec<pii> xs;
ll ans=0;
for(auto &z : mp){
bl+=z.s;jt+=cons[z.f];
cons[z.f]=jt;id[z.f]=tt++;
///from
xs.pb({z.f,bl});
}
vec<int> vc;
for(int i=0;i+1<sz(xs);i++){
if(xs[i].s>0) ans+=1ll*(xs[i+1].f-xs[i].f)*xs[i].s,xs[i].s=0,cons[xs[i].f]++;
}
for(int i=0;i<sz(xs);i++) make(i);
vec<array<int,3>> arr;
for(int i=0;i<sz(s);i++) arr.pb({0,id[s[i]],id[t[i]]});
for(int i=0;i+1<sz(xs);i++){
if(cons[xs[i].f]) arr.pb({0,i,i+1});
else arr.pb({xs[i+1].f-xs[i].f,i,i+1});
}
sort(all(arr));
for(auto &z : arr){
if(mg(z[1],z[2]))
ans+=z[0];
}
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... |