Submission #109368

#TimeUsernameProblemLanguageResultExecution timeMemory
109368JustasZAncient Books (IOI17_books)C++14
0 / 100
66 ms7544 KiB
#include "books.h" #include <bits/stdc++.h> #define pb push_back #define x first #define y second #define all(a) a.begin(), a.end() #define sz(a) (int)a.size() #define rd() abs((int)rng()) using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; const int maxn = 1e6 + 100; const int mod = 1e9 + 7; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); int n, s, val[maxn], nxt; vector<vector<int> > edge; vector<int>p; vector<pair<int, pii> > edges; ll ans; int par[maxn]; int root(int v) { return v == par[v] ? v : par[v] = root(par[v]); } bool unite(int a, int b) { a = root(a), b = root(b); if(a == b) return false; par[b] = a; return true; } ll minimum_walk(vector<int>perm, int S) { s = S; p = perm; n = sz(p); set<int>one; for(int i = 0; i < n; i++) { if(!val[i]) { int col = ++nxt, cur = i, cnt = 0, chk = 0; while(!val[cur]) { if(cur == s) chk = 1; cnt++; val[cur] = col; ans += abs(p[cur] - cur); cur = p[cur]; } if(cnt == 1 && !chk) one.insert(col); } } edge = vector<vector<int> > (n, vector<int>(n, mod)); for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++) { if(val[i] != val[j]) edge[val[i]][val[j]] = min(edge[val[i]][val[j]], j - i); } } for(int i = 1; i <= nxt; i++) par[i] = i; for(int i = 1; i <= nxt; i++) for(int j = i + 1; j <= nxt; j++) edges.pb({edge[i][j], {i, j}}); sort(all(edges)); for(auto aa : edges) { if(one.count(aa.y.x) || one.count(aa.y.y)) continue; if(unite(aa.y.x, aa.y.y)) ans += aa.x * 2; } return ans; } void clr() { edges.clear(); memset(val, 0, sizeof val); } /*int main() { ios_base::sync_with_stdio(false), cin.tie(0); cout << minimum_walk({0, 3, 5, 2, 4, 1}, 0); return 0; }*/
#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...