#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#include "grader.cpp"
#else
#include "books.h"
#endif
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int INF = 1e9;
const int SIZE = 1e6 + 5;
int n, L, R;
int p[SIZE];
bool vs[SIZE];
int l[SIZE], r[SIZE];
int d[SIZE], in[SIZE], out[SIZE], dis[SIZE];
ll minimum_walk(vector<int> p_, int s) {
n = p_.size();
s++;
FOR (i, 1, n) {
p[i] = p_[i - 1] + 1;
in[i] = out[i] = vs[i] = 0;
}
FOR (i, 1, n) {
if (i < p[i]) {
out[i]++, out[p[i]]--;
in[i + 1]++, in[p[i] + 1]--;
d[i]++, d[p[i]]--;
} else {
out[p[i] + 1]++, out[i + 1]--;
in[p[i]]++, in[i]--;
d[p[i]]++, d[i]--;
}
}
FOR (i, 1, n) {
in[i] += in[i - 1];
out[i] += out[i - 1];
d[i] += d[i - 1];
}
L = 1, R = n;
while (R > s && d[R - 1] == 0) R--;
while (L < s && d[L] == 0) L++;
if (L == R) return 0;
FOR (i, L, R) if (vs[i] == 0) {
int mn = n, mx = 1, x = i;
vector<int> v;
while (vs[x] == 0) {
mn = min(mn, x);
mx = max(mx, x);
v.pb(x), vs[x] = 1, x = p[x];
}
for (int x : v) l[x] = mn, r[x] = mx;
}
FOR (i, L, R) {
int cl = l[i], cr = r[i];
FOR (j, l[i], r[i]) {
cl = min(cl, l[j]);
cr = max(cr, r[j]);
}
while (cl < l[i] || cr > r[i]) {
int ml = cl, mr = cr;
FOR (j, cl, l[i] - 1) {
ml = min(ml, l[j]);
mr = max(mr, r[j]);
}
FOR (j, r[i] + 1, cr) {
ml = min(ml, l[j]);
mr = max(mr, r[j]);
}
l[i] = cl, r[i] = cr;
cl = ml, cr = mr;
}
}
ll ans = 0;
FOR (i, L, R) ans += out[i];
l[L - 1] = l[R + 1] = L - 1;
r[L - 1] = r[R + 1] = R + 1;
FOR (i, L - 1, R + 1) dis[i] = INF;
queue<int> q;
FOR (i, l[s], r[s]) dis[i] = 0, q.push(i);
while (q.size()) {
int pos = q.front();
q.pop();
auto upd = [&](int l, int r, int d) {
FOR (i, l, r) if (d < dis[i]) {
dis[i] = d;
q.push(i);
}
};
if (l[pos] >= L) {
int p = l[pos];
upd(l[p - 1], r[p - 1], dis[pos] + 1);
}
if (r[pos] <= R) {
int p = r[pos];
upd(l[p + 1], r[p + 1], dis[pos] + 1);
}
}
ans += 2 * dis[L - 1];
return ans;
}
/*
in1
4 0
0 2 3 1
out1
6
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Incorrect |
1 ms |
12636 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Incorrect |
1 ms |
12636 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Incorrect |
1 ms |
12636 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
12636 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3306' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Incorrect |
1 ms |
12636 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
4 |
Halted |
0 ms |
0 KB |
- |