Submission #123197

#TimeUsernameProblemLanguageResultExecution timeMemory
123197SirCenessAncient Books (IOI17_books)C++14
50 / 100
296 ms87560 KiB
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define inside sl<=l&&r<=sr #define outside r<sl||sr<l #define inf 1000000009 using namespace std; typedef long long ll; int n; vector<int> arr; vector<ll> numara; vector<ll> beg; vector<ll> en; vector<pair<int, pair<int, int> > > edges; int num = 0; vector<pair<ll, ll> > minler; vector<int> dsu; int find(int node){ if (dsu[node] == node) return node; int ans = find(dsu[node]); return dsu[node] = ans; } void unionn(int node1, int node2){ node1 = find(node1); node2 = find(node2); if (node1 == node2) return; dsu[node1] = node2; } ll minimum_walk(vector<int> p, int s){ arr = p; n = p.size(); numara.resize(n); int sonn = n-1; while (p[sonn] == sonn && sonn > 0) sonn--; if (sonn == 0) return 0; int strt = 0; while (p[strt] == strt) strt++; ll ans = 0; for (int i = 0; i < n; i++) numara[i] = -1; for (int i = 0; i < n; i++){ if (numara[i] == -1 && p[i] != i){ int node = i; int lmost = node; int rmost = node; while (numara[node] == -1){ numara[node] = num; ans += abs(p[node] - node); node = p[node]; lmost = min(lmost, node); rmost = max(rmost, node); } beg.pb(lmost); en.pb(rmost); num++; } } //for (int i = 0; i < n; i++) cout << numara[i] << " "; cout << endl; vector<list<int> > begs(n); vector<list<int> > ens(n); for (int i = 0; i < num; i++){ begs[beg[i]].pb(i); ens[en[i]].pb(i); } set<int> open; int flag = 1; vector<int> trumu(num); for (int i = 0; i < num; i++) trumu[i] = 0; for (int i = strt; i <= sonn; i++){ for (list<int>::iterator it = begs[i].begin(); it != begs[i].end(); ++it){ if (open.size() == 0) trumu[*it] = 1; open.insert(*it); } int foo = 0; for (list<int>::iterator it = ens[i].begin(); it != ens[i].end(); ++it){ //assert(open.find(*it) != open.end()); open.erase(open.find(*it)); if (trumu[*it] == 1) foo = 1; } if (open.size() == 0) ans += 2; if (i == s && open.size() == 0){ flag = 0; } if (foo){ for (set<int>::iterator it = open.begin(); it != open.end(); ++it){ trumu[*it] = 1; } } } ans -= 2; int mindist = inf; for (int i = s; i < n; i++){ if (numara[i] != -1 && trumu[numara[i]] == 1){ mindist = min(mindist, abs(i-s)); break; } } for (int i = s; i >= 0; i--){ if (numara[i] != -1 && trumu[numara[i]] == 1){ mindist = min(mindist, abs(s-i)); break; } } if (flag) ans += 2*mindist; /*cout << "sonn: " << sonn << endl; cout << "strt: " << strt << endl; cout << "mindist: " << mindist << endl;*/ 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...