Submission #983991

#TimeUsernameProblemLanguageResultExecution timeMemory
983991CSQ31Ancient Books (IOI17_books)C++17
50 / 100
109 ms34932 KiB
#include "books.h" #include<bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define se second #define sz(a) (int)(a.size()) #define all(a) a.begin(),a.end() typedef long long int ll; typedef pair<int,int> pii; const int MAXN = 1e6+1; int par[MAXN]; int mn[MAXN],mx[MAXN]; int find(int x){ if(x==par[x])return x; return par[x] = find(par[x]); } void unite(int a,int b){ a = find(a); b = find(b); if(a==b)return; mx[b] = max(mx[b],mx[a]); mn[b] = min(mn[b],mn[a]); par[a] = b; } ll minimum_walk(vector<int> p, int s) { int n = sz(p); ll ans = 0; for(int i=0;i<n;i++)ans+=abs(i-p[i]); for(int i=0;i<n;i++){ par[i] = i; mn[i] = min(i,p[i]); mx[i] = max(i,p[i]); } for(int i=0;i<n;i++){ int l = min(i,p[i]); int r = max(i,p[i]); if(l==r)continue; while(true){ l = find(l); if(l >= r)break; unite(l,l+1); l = find(l); } } int cur = 0; while(true){ int nxt = cur; while(nxt < n){ if(find(nxt) > nxt)break; nxt++; } if(nxt == n)break; ans += 2*(nxt - cur); cur = find(nxt); } 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...