Submission #20750

#TimeUsernameProblemLanguageResultExecution timeMemory
20750aintaRoller Coaster Railroad (IOI16_railroad)C++14
100 / 100
279 ms24408 KiB
#include "railroad.h" #include <stdio.h> #include <algorithm> #include <vector> using namespace std; int n, X[410000], cnt, Ideg[410000], Odeg[410000], P[410000]; bool v[410000]; int UF[410000]; struct Edge{ int a, b, c; bool operator<(const Edge &p)const{ return c<p.c; } }w[410000]; int Find(int a){ if(a==UF[a])return a; return UF[a] = Find(UF[a]); } void Merge(int a, int b){ a=Find(a),b=Find(b); if(a!=b)UF[a]=b; } void Add(int a, int b, int c){ if(c<=0)return; Odeg[a]+=c, Ideg[b]+=c; Merge(a,b); } long long plan_roller_coaster(vector<int> s, vector<int> t) { n = s.size(); int i; for(i=0;i<n;i++){ X[++cnt] = s[i]; X[++cnt] = t[i]; } X[++cnt] = 0, X[++cnt] = 2e9; sort(X+1,X+cnt+1); for(i=1;i<=cnt;i++)UF[i] = i; for(i=0;i<n;i++){ s[i] = lower_bound(X+1,X+cnt+1,s[i])-X; t[i] = lower_bound(X+1,X+cnt+1,t[i])-X; Add(s[i],t[i],1); } Add(cnt,1,1); int sum = 0; long long res = 0; for(i=1;i<=cnt;i++){ sum += Ideg[i] - Odeg[i]; P[i] = -sum; if(sum < 0){ res += 1ll * (-sum) * (X[i+1]-X[i]); } } for(i=1;i<cnt;i++){ Add(i+1,i,P[i]); } for(i=1;i<cnt;i++){ Add(i,i+1,Ideg[i]-Odeg[i]); w[i].a=i,w[i].b=i+1,w[i].c=X[i+1]-X[i]; } sort(w+1,w+cnt); for(i=1;i<cnt;i++){ if(Find(w[i].a)!=Find(w[i].b)){ res += w[i].c; Merge(w[i].a,w[i].b); } } return res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...