Submission #279997

#TimeUsernameProblemLanguageResultExecution timeMemory
279997eohomegrownappsRoller Coaster Railroad (IOI16_railroad)C++14
30 / 100
346 ms57616 KiB
#include "railroad.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; int INF = 1e9; vector<vector<int>> adjlist; vector<int> comind; vector<bool> visited; void dfs(int ind, int com){ //cout<<ind<<' '<<com<<'\n'; visited[ind]=true; comind[ind]=com; for (int i : adjlist[ind]){ if (!visited[i]){ dfs(i,com); } } } struct UFDS{ vector<int> parent; int n; UFDS(int _n){ n=_n; parent.resize(n); for (int i = 0; i<n; i++){ parent[i]=i; } } int root(int x){ if (x==parent[x]){return x;} return parent[x]=root(parent[x]); } void connect(int a, int b){ int ra = root(a); int rb = root(b); if (ra!=rb){ parent[ra]=rb; } } bool connected(int a, int b){ return root(a)==root(b); } }; long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) { vector<int> ind2coord; for (int i : s){ ind2coord.push_back(i); } for (int i : t){ ind2coord.push_back(i); } ind2coord.push_back(INF); ind2coord.push_back(1); sort(ind2coord.begin(),ind2coord.end()); ind2coord.erase(unique(ind2coord.begin(),ind2coord.end()),ind2coord.end()); s.push_back(INF); t.push_back(1); int n = ind2coord.size(); for (int i = 0; i<s.size(); i++){ s[i]=lower_bound(ind2coord.begin(),ind2coord.end(),s[i])-ind2coord.begin(); t[i]=lower_bound(ind2coord.begin(),ind2coord.end(),t[i])-ind2coord.begin(); } //start at 1, end at 1 int ans = 0; vector<int> edgedelta(n); for (int i = 0; i<s.size(); i++){ edgedelta[s[i]]++; edgedelta[t[i]]--; } int curdelta = 0; for (int i = 0; i<n-1; i++){ curdelta+=edgedelta[i]; if (curdelta<0){ //need to go ltr / speed up (fine) s.push_back(i); t.push_back(i+1); } if (curdelta>0){ //need to go rtl / slow down (not fine) ans+=curdelta*(ind2coord[i+1]-ind2coord[i]); s.push_back(i+1); t.push_back(i); } } //make connected components adjlist.resize(n); visited.resize(n); comind.resize(n); for (int i = 0; i<s.size(); i++){ adjlist[s[i]].push_back(t[i]); } int cptr = 0; for (int i = 0; i<n; i++){ if (!visited[i]){ dfs(i,cptr); cptr++; } } vector<pair<int,pair<int,int>>> edges; for (int i = 0; i<n-1; i++){ if (comind[i]!=comind[i+1]){ edges.push_back({ind2coord[i+1]-ind2coord[i],{comind[i],comind[i+1]}}); } } sort(edges.begin(), edges.end()); UFDS u(n); for (auto e : edges){ // are these connected? if (u.connected(e.second.first, e.second.second)){ continue; } ans+=e.first; u.connect(e.second.first, e.second.second); } return ans; }

Compilation message (stderr)

railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:68:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |     for (int i = 0; i<s.size(); i++){
      |                     ~^~~~~~~~~
railroad.cpp:77:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |     for (int i = 0; i<s.size(); i++){
      |                     ~^~~~~~~~~
railroad.cpp:101:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |     for (int i = 0; i<s.size(); 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...