제출 #266663

#제출 시각아이디문제언어결과실행 시간메모리
266663sealnot123Roller Coaster Railroad (IOI16_railroad)C++14
100 / 100
381 ms42212 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...