Submission #283913

#TimeUsernameProblemLanguageResultExecution timeMemory
283913ToMmyDongAncient Books (IOI17_books)C++11
0 / 100
1 ms384 KiB
#include "books.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; #define SZ(i) int(i.size()) #define ALL(i) i.begin(), i.end() #define MEM(i,a) memset(i, (a), sizeof(i)) #define X first #define Y second #define eb emplace_back #define pb push_back #ifdef tmd #define debug(...) fprintf(stderr,"#%d-(%s)=",__LINE__,#__VA_ARGS__);_do(__VA_ARGS__); template<typename T> void _do (T &&x) {cerr<<x<<endl;} template<typename T, typename ...S> void _do(T &&x, S &&...y) {cerr<<x<<", ";_do(y...);} template<typename IT> ostream& __printRng (ostream &os, IT bg, IT ed) { for (IT it=bg; it!=ed; it++) { if (it!=bg) os << "," << *it; else os << "{" << *it; } return os << "}"; } template<typename T> ostream &operator << (ostream &os, const pair<T,T> &v) { return os <<"{"<<v.X<<","<<v.Y<<"}"; } template<typename T> ostream &operator << (ostream &os, const vector<T> &v) { return __printRng(os, ALL(v)); } template<typename T> ostream &operator << (ostream &os, const set<T> &v) { return __printRng(os, ALL(v)); } #else #define debug(...) #endif const int MAXN = 1000006; bool vis[MAXN]; vector<int> to; void dfs (int nd, int &mn, int &mx, ll &sum) { vis[nd] = true; mn = min(mn, nd); mx = max(mx, nd); if (vis[to[nd]] == 0) dfs(to[nd], mn, mx, sum); sum += abs(to[nd] - nd); } long long minimum_walk(std::vector<int> p, int s) { int n = SZ(p); to = p; vector<pii> res; ll ans = 0; for (int i=0; i<n; i++) { if (!vis[i]) { int mn = n, mx = -1; ll sum = 0; dfs(i, mn, mx, sum); res.eb(mn, mx); ans += sum; } } debug(res); sort(ALL(res)); int lst = -1; int comp = 0; int L = 0; for (auto pa : res) { if (pa.X > lst) { assert(pa.X == lst+1); L = pa.X; comp++; } lst = max(lst, pa.Y); } return ans + comp - 1 + L; }
#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...