Submission #296559

#TimeUsernameProblemLanguageResultExecution timeMemory
296559Aldas25Ancient Books (IOI17_books)C++14
0 / 100
13 ms9596 KiB
#include "books.h" #include <bits/stdc++.h> using namespace std; #define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr) #define FOR(i, a, b) for(int i = (a); i <= (b); i++) #define REP(n) FOR(O, 1, (n)) #define pb push_back #define f first #define s second typedef long double ld; typedef long long ll; typedef pair<int, int> pii; typedef pair<int, pii> piii; typedef vector<int> vi; typedef vector<pii> vii; typedef vector<ll> vl; typedef vector<piii> viii; const int MAXN = 1000100; int n; ll d[1100][1100]; int par[MAXN], sz[MAXN]; int find (int a) {return par[a] = par[a]==a ? a : find(par[a]);} bool same (int a, int b) {return find(a) == find(b);} void unite (int a, int b) { a = find(a), b = find (b); if (a == b) return; par[b] = a; sz[a] += sz[b]; } vector<pair<ll, pii>> edges; long long minimum_walk(std::vector<int> p, int s) { n = (int)p.size(); ll ret = 0ll; FOR(i,0, n-1) ret += abs(i - p[i]); FOR(i, 0, n-1) par[i] = i, sz[i] = 1; FOR(i, 0, n-1) unite (i, p[i]); FOR(i, 0, n-1) FOR(j, 0, n-1) d[i][j] = n + 5; FOR(i, 0, n-1) FOR(j, i+1, n-1) { int pi = find(i), pj = find(j); d[pi][pj] = min(d[pi][pj], (ll)abs(j-i)); //d[pj][pi] = min(d[pj][pi], abs(j-i)); } FOR(i, 0, n-1) FOR(j, i+1, n-1) { if (find(i) != i || find(j) != j) continue; if (i != s && j != s && (sz[i] == 1 || sz[j] == 1)) continue; edges.pb({d[i][j], {i,j}}); } sort(edges.begin(), edges.end()); ll sum = 0ll; FOR(i, 0, n-1) par[i] = i; for (auto e : edges) { ll w = e.f; int u = e.s.f, v = e.s.s; if (same(u,v)) continue; unite(u,v); sum += w; } ret += 2*sum; return ret; }
#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...