제출 #284342

#제출 시각아이디문제언어결과실행 시간메모리
284342user202729Roller Coaster Railroad (IOI16_railroad)C++17
100 / 100
257 ms18552 KiB
// moreflags=grader.cpp #include "railroad.h" // 16 // is this cheating?... // again, I should have solved this problem earlier. #include<algorithm> #include<numeric> #if not LOCAL #define NDEBUG 1 #endif #include<cassert> struct Dsu{ std::vector<int> data; Dsu(int number): data(number){ while(number--) data[number]=number; } void join(int first, int sec){ first=root(first); sec=root(sec); data[first]=sec; } int root(int node){ return data[node]!=node? data[node]=root(data[node]): node; } }; long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) { std::vector<int> values; values.reserve(s.size()+t.size()); for(auto it: s) values.push_back(it); for(auto it: t) values.push_back(it); std::sort(begin(values), end(values)); values.resize(std::unique(begin(values), end(values))-values.begin()); std::vector<int> degree(values.size()); Dsu dsu((int)values.size()); for(int i=0; i<(int)s.size(); ++i){ auto const a=int(std::lower_bound(begin(values), end(values), s[i])-begin(values)); auto const b=int(std::lower_bound(begin(values), end(values), t[i])-begin(values)); degree[a]++; degree[b]--; dsu.join(a, b); } degree[0]--; // add a huge edge downward dsu.join(0, (int)values.size()-1); std::partial_sum(begin(degree), end(degree), degree.begin()); degree.pop_back(); struct Edge{int len, first, sec;}; std::vector<Edge> pending; int64_t result{}; for(int index=0; index<(int)degree.size(); ++index){ auto const edgeLen=values[index+1]-values[index]; if(degree[index]>0) result+=int64_t(edgeLen)*degree[index]; if(degree[index]!=0) dsu.join(index, index+1); else if(dsu.root(index)!=dsu.root(index+1)) pending.push_back({edgeLen, index, index+1}); } std::sort(begin(pending), end(pending),[&](Edge first, Edge sec){return first.len<sec.len;}); for(auto [len, first, sec]: pending) if(dsu.root(first)!=dsu.root(sec)){ dsu.join(first, sec); result+=len; } for(int i=0; i<(int)dsu.data.size(); ++i) assert(dsu.root(i)==dsu.root(0)); return result; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...