Submission #46922

#TimeUsernameProblemLanguageResultExecution timeMemory
46922SpaimaCarpatilorAncient Books (IOI17_books)C++17
100 / 100
1037 ms249660 KiB
#include "books.h" #include<bits/stdc++.h> using namespace std; int nr, N, BG, p[1000009], d[1000009], mars[1000009]; set < int > S; vector < int > v[1000009]; void add (int node, int newD) { if (d[node] != -1) return ; d[node] = newD, v[newD].push_back (node), S.erase (node); int l = node, r = p[node]; if (l > r) swap (l, r); while (1) { auto it = S.lower_bound (l); if (it == S.end () || *it > r) break; add (*it, newD); } } long long minimum_walk (vector < int > pp, int ss) { N = pp.size (), BG = ss + 1; for (int i=1; i<=N; i++) p[i] = pp[i - 1] + 1; long long ans = 0; int x = 0, y = 0; for (int i=1; i<=N; i++) { int curr = i - p[i]; if (curr < 0) curr = -curr; ans += curr; int l = i, r = p[i]; if (l > r) swap (l, r); mars[l] ++, mars[r] --; if (i != p[i] || i == BG) { if (x == 0) x = i; y = i; } } for (int i=1; i<N; i++) mars[i] += mars[i - 1]; if (mars[BG - 1] != 0 || mars[BG] != 0) { int lft = BG, rgt = BG; while (mars[rgt] > 0) rgt ++; while (mars[lft - 1] > 0) lft --; for (int i=lft; i<=rgt; i++) S.insert (i), d[i] = -1; add (BG, 0); for (int i=0; !v[i].empty (); i++) for (auto it : v[i]) { if (it > 1) add (it - 1, i + 1); if (it < N) add (it + 1, i + 1); } int maxi = 0; for (int i=lft; i<=rgt; i++) maxi = max (maxi, d[i]); ans += 2LL * maxi; } for (int i=x; i<y; i++) if (mars[i] == 0) ans += 2; return ans; }
#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...