제출 #1070346

#제출 시각아이디문제언어결과실행 시간메모리
1070346amirhoseinfar1385Roller Coaster Railroad (IOI16_railroad)C++17
30 / 100
136 ms25304 KiB
#include "railroad.h" #include<bits/stdc++.h> using namespace std; const int maxn=600000+10; vector<int>allind; int n,vorod[maxn],khoroj[maxn],inf=1e9+5; struct dsu{ int par[maxn]; dsu(){ for(int i=0;i<maxn;i++){ par[i]=i; } } int root(int u){ if(u==par[u]){ return u; } return par[u]=root(par[u]); } int con(int u,int v){ u=root(u);v=root(v); if(u==v){ return 0; } par[u]=v; return 1; } }ds,ds2; long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) { s.push_back(inf); t.push_back(1); n=(int)s.size(); for(int i=0;i<n;i++){ allind.push_back(s[i]); allind.push_back(t[i]); } sort(allind.begin(),allind.end()); allind.resize(unique(allind.begin(),allind.end())-allind.begin()); for(int i=0;i<n;i++){ int pp=lower_bound(allind.begin(),allind.end(),s[i])-allind.begin(); khoroj[pp]++; int p=lower_bound(allind.begin(),allind.end(),t[i])-allind.begin(); vorod[p]++; ds.con(p,pp); } long long mainres=0,vo=0,ko=0; for(int i=0;i<(int)allind.size();i++){ ko+=khoroj[i]; vo+=vorod[i]; if(ko>vo){ mainres+=ko-vo; } if(ko!=vo){ ds.con(i,i+1); } } vector<pair<long long ,pair<long long ,long long>>>allv; for(int i=0;i<(int)allind.size()-1;i++){ if(ds.root(i)!=ds.root(i+1)){ allv.push_back(make_pair(allind[i+1]-allind[i],make_pair(ds.root(i),ds.root(i+1)))); } } sort(allv.begin(),allv.end()); for(int i=0;i<(int)allv.size();i++){ if(ds2.con(allv[i].second.first,allv[i].second.second)){ mainres+=allv[i].first; } } return mainres; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...