Submission #109402

#TimeUsernameProblemLanguageResultExecution timeMemory
109402JustasZAncient Books (IOI17_books)C++14
12 / 100
47 ms31736 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, dist[maxn]; vector<vector<int> > edge; vector<int>p; vector<pii>adj[maxn]; ll ans; void clr() { for(int i = 1; i <= nxt; i++) adj[i].clear(); ans = 0; nxt = 0; memset(val, 0, sizeof val); } ll minimum_walk(vector<int>perm, int S) { clr(); 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); } } vector<int>belong[nxt + 1]; for(int i = 0; i < n; i++) belong[val[i]].pb(i); edge = vector<vector<int> > (nxt + 1, vector<int>(nxt + 1, 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++) { for(int j = 1; j <= nxt; j++) { if(i == j) continue; for(int a : belong[i]) { for(int b : belong[j]) { if((a < b && p[a] > b) || (a > b && p[a] < b)) edge[i][j] = 0; } } } } for(int i = 1; i <= nxt; i++) for(int j = i + 1; j <= nxt; j++) { if((one.count(i) && i != val[s]) || (one.count(j) && j != val[s])) { continue; } adj[i].pb({j, edge[i][j]}); adj[j].pb({i, edge[j][i]}); } for(int i = 1; i <= nxt; i++) { for(int j = 1; j <= nxt; j++) { if(i == j) continue; /*for(pii to : adj[i]) { cout << i << " " << to.x << " " << to.y << endl; }*/ } } set<pii>se; for(int i = 1; i <= nxt; i++) dist[i] = mod; se.insert({dist[val[s]] = 0, val[s]}); while(sz(se)) { int v = se.begin()->second; if(dist[v] != mod) ans += dist[v] * 2; //cout << v << " " << dist[v] << endl; se.erase(se.begin()); for(pii to : adj[v]) { if(dist[to.x] > to.y) { //cout << v << " " << to.x << " " << to.y << endl; if(dist[to.x] != mod) se.erase({dist[to.x], to.x}); dist[to.x] = to.y; se.insert({dist[to.x], to.x}); } } } //int add = mod; //for(int i = 1; i <= nxt; i++) // if(i != startcol) add = min(add, edge[i][startcol]); //ans += add; //cout << "Startcol: " << val[s] << endl; return ans; } /*int main() { ios_base::sync_with_stdio(false), cin.tie(0); vector<int>perm; ifstream fin("in.txt"); int n, s; fin >> n >> s; perm.resize(n); for(int i = 0; i < n; i++) fin >> perm[i]; cout << minimum_walk(perm, s) << endl; //cout << minimum_walk({3, 2, 1, 0}, 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...