Submission #906806

#TimeUsernameProblemLanguageResultExecution timeMemory
906806mathneticRoller Coaster Railroad (IOI16_railroad)C++17
100 / 100
505 ms33172 KiB
#include <cassert> #include <cstdio> #include <algorithm> #include <unordered_map> #include "railroad.h" using namespace std; typedef pair<int,int> ii; typedef long long ll; unordered_map<int, int> uf; int fnd(int x){ if(uf[x]==0) uf[x]=x; if(x==uf[x]) return x; else return uf[x]=fnd(uf[x]); } void un(int x, int y){ uf[fnd(x)]=fnd(y); } bool cmp(ii a, ii b){ return (a.second-a.first)<(b.second-b.first); } ll plan_roller_coaster(vector<int> s, vector<int> t){ ll sol=0; s.push_back(1e9); t.push_back(1); int n = (int) s.size(); vector<ii> e, ed; for(int i=0;i<n;++i){ un(s[i], t[i]); e.emplace_back(s[i], 1); e.emplace_back(t[i], -1); } sort(e.begin(), e.end()); int b=0; for(int i=0, j=0;i<e.size();i=j){ for(;j<e.size() && e[j].first==e[i].first;++j){ b+=e[j].second; } if(j<e.size()){ if(b>0){ sol+=(ll)b*(e[j].first-e[i].first); } if(b==0){ ed.emplace_back(e[i].first, e[j].first); }else{ un(e[j].first, e[i].first); } } } sort(ed.begin(), ed.end(), cmp); for(auto i:ed){ if(fnd(i.first)!=fnd(i.second)){ sol+=i.second-i.first; un(i.first, i.second); } } return sol; }

Compilation message (stderr)

railroad.cpp: In function 'll plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:42:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     for(int i=0, j=0;i<e.size();i=j){
      |                      ~^~~~~~~~~
railroad.cpp:43:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |         for(;j<e.size() && e[j].first==e[i].first;++j){
      |              ~^~~~~~~~~
railroad.cpp:46:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |         if(j<e.size()){
      |            ~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...