Submission #515132

#TimeUsernameProblemLanguageResultExecution timeMemory
515132SHZhangRoller Coaster Railroad (IOI16_railroad)C++14
100 / 100
150 ms17432 KiB
#include "railroad.h" #include <algorithm> #include <utility> using namespace std; vector<pair<int, int> > locs; int uf[400005]; int pre[400005]; int fin(int x) { if (uf[x] == x) return x; return uf[x] = fin(uf[x]); } void un(int x, int y) { x = fin(x); y = fin(y); if (x != y) uf[x] = y; } long long plan_roller_coaster(vector<int> s, vector<int> t) { int n = (int)s.size(); for (int i = 0; i < 2 * n; i++) { uf[i] = i; pre[i] = -1; } for (int i = 0; i < n; i++) locs.push_back(make_pair(s[i], i + 1)); for (int i = 0; i < n; i++) locs.push_back(make_pair(t[i], -i - 1)); sort(locs.begin(), locs.end()); for (int i = 0; i < 2 * n; i++) { int idx; if (locs[i].second < 0) { idx = -locs[i].second - 1; } else { idx = locs[i].second - 1; } //printf("! %d %d\n", locs[i].first, idx); if (pre[idx] != -1) { un(i, pre[idx]); //printf("! %d %d\n", i, pre[idx]); } else { pre[idx] = i; } } int rightcnt = 1; vector<pair<int, int> > edges; long long ans = 0; for (int i = 0; i < 2 * n; i++) { if (locs[i].second < 0) { rightcnt++; } else { rightcnt--; } if (!rightcnt) { edges.push_back( make_pair(locs[i+1].first - locs[i].first, i)); } else if (i != 2 * n - 1) { un(i, i+1); //printf("add %d %d\n", i, rightcnt); if (rightcnt < 0) ans += (long long)(-rightcnt) * (long long)(locs[i+1].first - locs[i].first); } } sort(edges.begin(), edges.end()); for (int i = 0; i < edges.size(); i++) { if (fin(edges[i].second) != fin(edges[i].second + 1)) { ans += (long long)edges[i].first; un(edges[i].second, edges[i].second + 1); } } return ans; }

Compilation message (stderr)

railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:67: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]
   67 |     for (int i = 0; i < edges.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...