Submission #139038

#TimeUsernameProblemLanguageResultExecution timeMemory
139038hamzqq9Roller Coaster Railroad (IOI16_railroad)C++14
30 / 100
1038 ms34300 KiB
#include "railroad.h" #include<bits/stdc++.h> #define st first #define nd second #define pb push_back #define pf push_front #define ppb pop_back #define ppf pop_front #define umax(x,y) x=max(x,y) #define umin(x,y) x=min(x,y) #define ll long long #define ii pair<int,int> #define iii pair<int,ii> #define iiii pair<ii,ii> #define sz(x) ((int) x.size()) #define orta ((bas+son)/2) #define all(x) x.begin(),x.end() #define inf 1000000000 #define MOD 1000000007 #define N 400005 #define LOG 20 #define KOK 300 #define EPS 0.0000001 using namespace std; int n,cnt; vector<int> pre,dad; int tut[N]; struct edge { int a,b,c; }; int find(int x) {return dad[x]=(x==dad[x]?x:find(dad[x]));} void merge(int x,int y) { dad[find(x)]=find(y); } long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) { map<int,int> mp; n=sz(s); for(int i=0;i<n;i++) { mp[s[i]]=1; mp[t[i]]=1; } mp[inf]=1; for(auto& x:mp) { x.nd=++cnt; tut[cnt]=x.st; } dad=pre=vector<int>(cnt+5,0); for(int i=1;i<=cnt;i++) dad[i]=i; for(int i=0;i<n;i++) { // cerr<<s[i]<<" "<< t[i]<<"\n"; s[i]=mp[s[i]]; t[i]=mp[t[i]]; merge(s[i],t[i]); // cerr<< "to : \n"; // cerr<<s[i] <<" "<<t[i]<<"\n"; // cerr<<"-------------------\n"; if(s[i]<=t[i]) { pre[s[i]]++; pre[t[i]]--; } else { pre[t[i]]--; pre[s[i]]++; } } ll ans=0; vector<edge> q; for(int i=1;i<cnt;i++) { pre[i]+=pre[i-1]; // cerr<<i << " " << pre[i] <<"\n"; if(pre[i]>1) { ans+=pre[i]-1; } if(pre[i]!=1) merge(i,i+1); if(find(i)!=find(i+1)) q.pb({i,i+1,tut[i+1]-tut[i]}); } sort(all(q),[](edge x,edge y) { return x.c<y.c; }); for(auto x:q) { if(find(x.a)!=find(x.b)) { merge(x.a,x.b); ans+=x.c; } } 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...