# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1058698 | shenfe1 | Ancient Books (IOI17_books) | C++17 | 2021 ms | 88928 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "books.h"
#pragma GCC optimize("Ofast")
using namespace std;
#define pb push_back
#define pii pair<int,int>
#define sz(s) (int)s.size()
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define ll long long
const int MAX=1e6+10;
int n;
vector<int> g[MAX];
bool use[MAX];
vector<int> vec;
int pref[MAX];
ll minimum_walk(vector<int> p,int s){
n=sz(p);
while(n>1&&p[n-1]==n-1)n--;
ll ans=0;
for(int i=0;i<n;i++){
ans+=abs(i-p[i]);
}
vector<vector<int>> x;
vector<pii> seg;
for(int i=0;i<n;i++){
if(!use[i]){
vec.clear();
int v=i;
while(!use[v]){
vec.pb(v);
use[v]=1;
v=p[v];
}
sort(all(vec));
seg.pb({vec[0],vec.back()});
x.pb(vec);
}
}
int L,R;
for(vector<int> v:x){
for(int f:v){
if(f==s){
L=v[0],R=v.back();
}
}
}
while(R-L+1!=n){
bool ok=0;
for(int i=0;i<sz(seg);i++){
auto [l,r]=seg[i];
for(int pos:x[i]){
if(L<=pos&&pos<=R){
if(l<L||r>R)ok=1;
L=min(L,l);
R=max(R,r);
}
}
}
if(!ok){
if(L-1>=0){
L--;
ans+=2;
}
if(R+1<n){
R++;
ans+=2;
}
}
}
return ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |