Submission #1037353

#TimeUsernameProblemLanguageResultExecution timeMemory
103735312345678Roller Coaster Railroad (IOI16_railroad)C++17
100 / 100
489 ms39744 KiB
#include "railroad.h" #include <bits/stdc++.h> using namespace std; const int nx=4e5+5; int n, T, qs[nx], dsu[nx], rv[nx]; long long ans; map<int, int> mp; int find(int x) { if (dsu[x]==x) return x; return dsu[x]=find(dsu[x]); } long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) { n=s.size(); for (int i=0; i<n; i++) mp[s[i]]=mp[t[i]]=0; for (auto &[x, y]:mp) y=++T, rv[y]=x; for (int i=1; i<=mp.size(); i++) dsu[i]=i; for (int i=0; i<n; i++) qs[mp[s[i]]]++, qs[mp[t[i]]]--, dsu[find(mp[s[i]])]=find(mp[t[i]]); for (int i=1; i<mp.size(); i++) { qs[i]+=qs[i-1]; if (qs[i]>1) ans+=(long long)(rv[i+1]-rv[i])*(qs[i]-1), dsu[find(i)]=find(i+1); if (qs[i]<1) dsu[find(i)]=find(i+1); } dsu[find(1)]=find(mp.size()); priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; for (int i=1; i<mp.size(); i++) pq.push({rv[i+1]-rv[i], i}); while (!pq.empty()) { auto [w, u]=pq.top(); pq.pop(); if (find(u)==find(u+1)) continue; ans+=w; dsu[find(u)]=find(u+1); } return ans; }

Compilation message (stderr)

railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:22:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::map<int, int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |     for (int i=1; i<=mp.size(); i++) dsu[i]=i;
      |                   ~^~~~~~~~~~~
railroad.cpp:24:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::map<int, int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     for (int i=1; i<mp.size(); i++)
      |                   ~^~~~~~~~~~
railroad.cpp:32:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::map<int, int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |     for (int i=1; i<mp.size(); i++) pq.push({rv[i+1]-rv[i], i});
      |                   ~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...