// Source: https://oj.uz/problem/view/IOI17_books
//
#include "books.h"
#include "bits/stdc++.h"
using namespace std;
#define s second
#define f first
#define pb push_back
typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<pii> vpii;
typedef vector<ll> vi;
#define FOR(i, a, b) for (ll i = (a); i<b; i++)
bool ckmin(ll& a, ll b){ return b < a ? a = b, true : false; }
bool ckmax(ll& a, ll b){ return b > a ? a = b, true : false; }
const ll N = 1e6 + 10;
ll ri[N], le[N];
bool vis[N];
vector<int> nxt;
ll n;
ll l,r ;
void extend() {
ll LL = nxt[l];
while (LL < l && l > 0) {
l--;
LL = min(LL, (ll) nxt[l]);
}
ll rr = nxt[r];
while (r < rr && r < n-1) {
r++;
rr = max(rr, (ll)nxt[r]);
}
}
ll minimum_walk(vector<int> p, int s) {
n = p.size();
l = r = s;
nxt = p;
ll ans = 0;
FOR(i, 0, n) ans += abs(i - p[i]);
ll st = 0;
for (;st < n;st++) if (p[st] != st) break;
ll en = n-1;
for (;en >= 0;en--) if (p[en] != en) break;
if (st >= en) return 0;
while (l > st || r < en) {
extend();
// cout << l << r << endl;
ll lo = l;
ll ro = r;
ll cr = 0;
while (l == lo && r < en) {
cr+=2;r++;
// cout << 'd' << endl;
extend();
}
bool rw = (l != lo);
l = lo;r = ro;
ll cl = 0;
while (r == ro && l > st) {
// cout << l << endl;
cl+=2;l--;extend();
}
bool lw = (r != ro);
// assert(lw ^ rw);
if (!lw || !rw) {
ans += cl + cr;
break;
}
ans += min(cl, cr);
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// FOR(i, 0, 8) {
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// ll n = 100;
// vi here(n);
// FOR(i, 0, n) here[i] = i;
// shuffle(here.begin(), here.end(), rng);
// FOR(i, 0, n) cout << here[i] << ' ';
// cout << endl;
ll res = minimum_walk({0, 2, 4, 5, 1, 6, 3}, 0);
cout << res << endl;
}
Compilation message
/usr/bin/ld: /tmp/ccNOcXYG.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccoHG6KI.o:books.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status