Submission #782420

#TimeUsernameProblemLanguageResultExecution timeMemory
782420GusterGoose27Ancient Books (IOI17_books)C++17
12 / 100
1 ms312 KiB
#include "books.h" #include <bits/stdc++.h> using namespace std; const int MAXN = 1e6+5; typedef long long ll; typedef pair<int, int> pii; bool vis[MAXN]; int n; int _abs(int v) { return v < 0 ? -v : v; } ll minimum_walk(vector<int> p, int s) { ll ans = 0; n = p.size(); for (int i = 0; i < n; i++) ans += _abs(p[i]-i); vector<pii> intervals; for (int i = 0; i < n; i++) { if (vis[i]) continue; int cur = i; int l = i, r = i; do { l = min(l, cur); r = max(r, cur); vis[cur] = 1; cur = p[cur]; } while (cur != i); if (r > l || i == s) intervals.push_back(pii(l, r)); } sort(intervals.begin(), intervals.end()); // they are probably already sorted int curl = intervals[0].first; int curr = intervals[0].second; for (int i = 1; i < intervals.size(); i++) { assert(curr != intervals[i].first); if (curr > intervals[i].first) { curr = max(curr, intervals[i].second); continue; } ans += 2*intervals[i].first-curr; curl = intervals[i].first; curr = intervals[i].second; } return ans; }

Compilation message (stderr)

books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:38:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |  for (int i = 1; i < intervals.size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~~~~
books.cpp:36:6: warning: variable 'curl' set but not used [-Wunused-but-set-variable]
   36 |  int curl = intervals[0].first;
      |      ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...