이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "railroad.h"
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define SZ(a) (int)(a).size()
#define FOR(i, a, b) for(int i=(a); i<=(b); ++i)
#define ROF(i, a, b) for(int i=(a); i>=(b); --i)
#define make_unique(a) sort(all((a))), (a).resize(unique(all((a)))-(a).begin())
using namespace std;
typedef pair<int,int> PII;
typedef long long LL;
typedef double DD;
typedef long double LD;
typedef pair<LL,LL> PLL;
typedef pair<DD,DD> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;
const int N = 400004;
int n;
LL plan_roller_coaster(VI s, VI t) {
int n = SZ(s);
vector<int> cmp;
FOR(i, 0, n-1) cmp.pb(s[i]), cmp.pb(t[i]);
cmp.pb(0); cmp.pb(1e9+2);
make_unique(cmp);
FOR(i, 0, n-1){
s[i] = lower_bound(all(cmp), s[i])-cmp.begin();
t[i] = lower_bound(all(cmp), t[i])-cmp.begin();
}
LL ans = 0;
int m = SZ(cmp);
vector<int> dp(m);
vector<VI> g(m);
FOR(i, 0, n-1){
g[s[i]].pb(t[i]);
dp[s[i]]++; dp[t[i]]--;
//printf("(%d, %d)\n",s[i],t[i]);
}
FOR(i, 0, m-2){
if(i != 0) dp[i] += dp[i-1];
//printf("%d ",dp[i]);
if(dp[i] > 1) ans += (dp[i]-1)*1ll*(cmp[i+1]-cmp[i]), g[i+1].pb(i);
if(dp[i] < 1) g[i].pb(i+1);
}
//puts("");
//printf("%lld\n",ans);
// mst lets gooo
queue<int> bfs;
vector<int> comp(m);
int cnt = 0;
FOR(i, 0, m-1){
if(comp[i]) continue;
++cnt;
comp[i] = cnt;
bfs.push(i);
while(!bfs.empty()){
int u = bfs.front();
bfs.pop();
for(int e : g[u]){
if(comp[e]) continue;
comp[e] = cnt;
bfs.push(e);
}
}
}
vector< pair<int,PII> > edges; // for mst
vector<int> dsu(cnt+1);
FOR(i, 1, cnt) dsu[i] = i;
function<int(int)> find = [&](int i){
//printf("==%d %d\n",dsu[i],i);
if(dsu[i] == i) return i;
return dsu[i] = find(dsu[i]);
};
FOR(i, 1, m-2){
//printf("[%d]\n",comp[i]);
if(comp[i-1] != comp[i]){
edges.pb({cmp[i]-cmp[i-1], PII(comp[i-1], comp[i])});
}
if(comp[i] != comp[i+1]){
edges.pb({cmp[i+1]-cmp[i], PII(comp[i+1], comp[i])});
}
}
sort(all(edges));
for(auto &f : edges){
LL cost = f.x;
int a, b;
tie(a, b) = f.y;
if(find(a) != find(b)){
ans += cost;
dsu[find(a)] = find(b);
}
}
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... |