#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ull mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double cur = rngesus(0, MASK(60) - 1);
cur /= MASK(60) - 1;
return l + cur * (r - l);
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
#include "books.h"
void update(pair<int, int> &x, pair<int, int> y){
minimize(x.first, y.first);
maximize(x.second, y.second);
}
void stretch_range(pair<int, int> &x, pair<int, int> &y, vector<pair<int, int>> &big_range){
while(x.first > y.first || x.second < y.second){
if (x.first > y.first){
update(y, big_range[--x.first]);
}
else{
update(y, big_range[++x.second]);
}
}
}
ll minimum_walk(vector<int> p, int s) {
int n = p.size();
ll ans = 0;
pair<int, int> min_range = {s, s};
for(int i = 0; i < n; ++i){
ans += abs(i - p[i]);
if (i !=p[i]){
minimize(min_range.first, i);
maximize(min_range.second, i);
}
}
vector<pair<int,int>> big_range(n, {-1, -1});
for(int i = 0; i < n; ++i){
if (big_range[i].first != -1) continue;
pair<int,int> mi = {i, i};
int j = p[i];
while(j != i){
minimize(mi.first, j); maximize(mi.second, j);
j = p[j];
}
do{
big_range[j] = mi;
j = p[j];
} while(j != i);
}
pair<int, int> cur = {s, s};
pair<int, int> desired = big_range[s];
while(true){
stretch_range(cur, desired, big_range);
if (cur.first <= min_range.first && cur.second >= min_range.second) break;
int left = 0, right = 0;
bool check = true;
// go left
pair<int, int> _cur = cur, _desired = desired;
while(_cur.first > min_range.first && _cur.second == cur.second){
update(_desired, big_range[--_cur.first]);
left++;
stretch_range(_cur, _desired, big_range);
}
if (_cur.second == cur.second) check = false;
// go right
_cur = cur, _desired = desired;
while(_cur.second < min_range.second && _cur.first == cur.first){
update(_desired, big_range[++_cur.second]);
right++;
stretch_range(_cur, _desired, big_range);
}
if (_cur.first == cur.first) check = false;
cur = _cur;
desired = _desired;
if (check){
ans += min(left, right) * 2;
}
else{
ans += (left + right) * 2;
break;
}
}
return ans;
}
# | 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... |