#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define f first
#define s second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)((x).size())
#define pb push_back
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<" "<<#__VA_ARGS__<<": ", _do(__VA_ARGS__)
template <typename T> void _do(T && x) {cerr<<x<<endl;}
template <typename T, typename ...S> void _do(T && x, S&&...y) {cerr<<x<<", "; _do(y...);}
#define IOS()
#else
#define bug(...)
#define IOS() ios::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#endif
const int maxn = 1e6+5;
bool seen[maxn];
int R[maxn], L[maxn];
bool st[maxn];
bool dv[maxn];
int RR[maxn], LL[maxn];
int par[maxn];
int find(int x) {return x == par[x]? x : par[x] = find(par[x]);}
void mrg(int a, int b) {
a = find(a); b = find(b);
if (a == b) return;
RR[a] = max(RR[a], RR[b]);
LL[a] = min(LL[a], LL[b]);
par[b] = a;
}
vector<int> here[maxn];
ll minimum_walk(vector<int> p, int s) {
memset(seen, 0, sizeof seen);
int n = SZ(p);
ll re= 0;
for (int i = 0; i<n; ++i) {
// --p[i];
re+= abs(i-p[i]);
}
// vector<pair<pii,int> > v;
for (int i = 0; i<n; ++i) {
if (!seen[i]) {
st[i] = 1;
seen[i] = 1;
R[i] = L[i] = i;
for (int x = p[i]; x != i; x = p[x]) seen[x] = 1, R[i] = max(R[i], x);
for (int x = p[i]; x != i; x = p[x]) R[x] = R[i], L[x] = L[i];
bug(i, L[i], R[i]);
LL[i] = L[i]; RR[i] =R[i];
}
}
int nmx = 0;
re -= 2;
for (int i = 0; i<n; ++i) {
nmx = max(nmx, R[i]);
// bug(L[i], R[i]);
if (nmx <= i) {
re += 2;
dv[i] = 1;
}
// if (i == s && nmx > i) {
// int ad = 0;
// for (int j = 1; ; ++j) {
// if (p[i+j] != i+j || p[i-j] != i-j) {
// ad = j; break;
// }
// }
// bug(ad);
// }
}
for (int i = 0; i<n; ++i) par[i] = i;
vector<pii> stk;
for (int i = 0; i<n; ++i) {
if (st[i]) {
while (!stk.empty() && i > stk.back().s) stk.pop_back();
int newl = i;
while (!stk.empty() && R[i] > stk.back().s) {
mrg(i, stk.back().f);
bug("merged", i, stk.back().f);
newl = stk.back().f;
stk.pop_back();
}
stk.pb({newl, R[i]});
}
}
for (int i = 0; i<n; ++i) {
if (st[i]) {
here[find(i)].pb(i);
for (int x = p[i]; x != i; x = p[x])
here[find(i)].pb(x), par[x] = find(i);
}
}
{
int l= L[s], r = R[s];
int lat = l, rat = l;
while (1) {
if (lat > l) {
--lat; l = min(l, L[lat]); r = max(r, R[lat]);
}
else if (rat < r) {
++rat; l = min(l, L[rat]); r = max(r, R[rat]);
}else{
assert(lat == l && rat ==r);
for (int i = 1; ; ++i) {
if (lat-i < 0 || rat+i>=n) {
goto fofoo;
}
if (RR[find(lat-i)] > r) {
// assert(L[lat-i] < l);
r = RR[find(lat-i)]; l = LL[find(lat-i)]; re += i*2; break;
}
if (LL[find(rat+i)] < l) {
// assert(R[rat+i] > r);
r = RR[find(rat+i)]; l = LL[find(rat+i)]; re += i*2; break;
}
}
}
}
}
fofoo:
for (int i = 0; i<n; ++i) {
if (p[i] == i && i != s) re -= 2;
else break;
}
for (int i = n-1; i>=0; --i) {
if (p[i] == i && i != s) re -= 2;
else break;
}
return re;
}
#ifdef BALBIT
signed main(){
IOS();
bug(minimum_walk({2,3,4,1,0},1));
// bug(minimum_walk({3,2,1,0},0));
// bug(minimum_walk({1,0,3,2},0));
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
24832 KB |
Output is correct |
2 |
Correct |
14 ms |
24832 KB |
Output is correct |
3 |
Correct |
15 ms |
24832 KB |
Output is correct |
4 |
Correct |
15 ms |
24832 KB |
Output is correct |
5 |
Correct |
15 ms |
24832 KB |
Output is correct |
6 |
Correct |
15 ms |
24832 KB |
Output is correct |
7 |
Correct |
14 ms |
24844 KB |
Output is correct |
8 |
Correct |
16 ms |
24832 KB |
Output is correct |
9 |
Correct |
15 ms |
24832 KB |
Output is correct |
10 |
Correct |
15 ms |
24832 KB |
Output is correct |
11 |
Correct |
15 ms |
24832 KB |
Output is correct |
12 |
Correct |
14 ms |
24960 KB |
Output is correct |
13 |
Correct |
15 ms |
24832 KB |
Output is correct |
14 |
Correct |
16 ms |
24832 KB |
Output is correct |
15 |
Correct |
17 ms |
24832 KB |
Output is correct |
16 |
Correct |
18 ms |
24832 KB |
Output is correct |
17 |
Correct |
15 ms |
24832 KB |
Output is correct |
18 |
Correct |
15 ms |
24832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
24832 KB |
Output is correct |
2 |
Correct |
14 ms |
24832 KB |
Output is correct |
3 |
Correct |
15 ms |
24832 KB |
Output is correct |
4 |
Correct |
15 ms |
24832 KB |
Output is correct |
5 |
Correct |
15 ms |
24832 KB |
Output is correct |
6 |
Correct |
15 ms |
24832 KB |
Output is correct |
7 |
Correct |
14 ms |
24844 KB |
Output is correct |
8 |
Correct |
16 ms |
24832 KB |
Output is correct |
9 |
Correct |
15 ms |
24832 KB |
Output is correct |
10 |
Correct |
15 ms |
24832 KB |
Output is correct |
11 |
Correct |
15 ms |
24832 KB |
Output is correct |
12 |
Correct |
14 ms |
24960 KB |
Output is correct |
13 |
Correct |
15 ms |
24832 KB |
Output is correct |
14 |
Correct |
16 ms |
24832 KB |
Output is correct |
15 |
Correct |
17 ms |
24832 KB |
Output is correct |
16 |
Correct |
18 ms |
24832 KB |
Output is correct |
17 |
Correct |
15 ms |
24832 KB |
Output is correct |
18 |
Correct |
15 ms |
24832 KB |
Output is correct |
19 |
Correct |
14 ms |
24832 KB |
Output is correct |
20 |
Correct |
16 ms |
24832 KB |
Output is correct |
21 |
Correct |
17 ms |
24832 KB |
Output is correct |
22 |
Correct |
15 ms |
24832 KB |
Output is correct |
23 |
Correct |
15 ms |
24832 KB |
Output is correct |
24 |
Correct |
15 ms |
24832 KB |
Output is correct |
25 |
Correct |
15 ms |
24832 KB |
Output is correct |
26 |
Correct |
15 ms |
24832 KB |
Output is correct |
27 |
Correct |
15 ms |
24832 KB |
Output is correct |
28 |
Correct |
15 ms |
24832 KB |
Output is correct |
29 |
Correct |
15 ms |
24832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
24832 KB |
Output is correct |
2 |
Correct |
14 ms |
24832 KB |
Output is correct |
3 |
Correct |
15 ms |
24832 KB |
Output is correct |
4 |
Correct |
15 ms |
24832 KB |
Output is correct |
5 |
Correct |
15 ms |
24832 KB |
Output is correct |
6 |
Correct |
15 ms |
24832 KB |
Output is correct |
7 |
Correct |
14 ms |
24844 KB |
Output is correct |
8 |
Correct |
16 ms |
24832 KB |
Output is correct |
9 |
Correct |
15 ms |
24832 KB |
Output is correct |
10 |
Correct |
15 ms |
24832 KB |
Output is correct |
11 |
Correct |
15 ms |
24832 KB |
Output is correct |
12 |
Correct |
14 ms |
24960 KB |
Output is correct |
13 |
Correct |
15 ms |
24832 KB |
Output is correct |
14 |
Correct |
16 ms |
24832 KB |
Output is correct |
15 |
Correct |
17 ms |
24832 KB |
Output is correct |
16 |
Correct |
18 ms |
24832 KB |
Output is correct |
17 |
Correct |
15 ms |
24832 KB |
Output is correct |
18 |
Correct |
15 ms |
24832 KB |
Output is correct |
19 |
Correct |
14 ms |
24832 KB |
Output is correct |
20 |
Correct |
16 ms |
24832 KB |
Output is correct |
21 |
Correct |
17 ms |
24832 KB |
Output is correct |
22 |
Correct |
15 ms |
24832 KB |
Output is correct |
23 |
Correct |
15 ms |
24832 KB |
Output is correct |
24 |
Correct |
15 ms |
24832 KB |
Output is correct |
25 |
Correct |
15 ms |
24832 KB |
Output is correct |
26 |
Correct |
15 ms |
24832 KB |
Output is correct |
27 |
Correct |
15 ms |
24832 KB |
Output is correct |
28 |
Correct |
15 ms |
24832 KB |
Output is correct |
29 |
Correct |
15 ms |
24832 KB |
Output is correct |
30 |
Correct |
410 ms |
48680 KB |
Output is correct |
31 |
Correct |
429 ms |
49248 KB |
Output is correct |
32 |
Correct |
219 ms |
85624 KB |
Output is correct |
33 |
Correct |
232 ms |
75872 KB |
Output is correct |
34 |
Correct |
236 ms |
75768 KB |
Output is correct |
35 |
Correct |
253 ms |
71544 KB |
Output is correct |
36 |
Correct |
221 ms |
64120 KB |
Output is correct |
37 |
Correct |
206 ms |
60284 KB |
Output is correct |
38 |
Correct |
194 ms |
58744 KB |
Output is correct |
39 |
Correct |
196 ms |
57852 KB |
Output is correct |
40 |
Correct |
202 ms |
52084 KB |
Output is correct |
41 |
Correct |
264 ms |
49456 KB |
Output is correct |
42 |
Correct |
244 ms |
49788 KB |
Output is correct |
43 |
Correct |
271 ms |
76408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
24832 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3586' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
24832 KB |
Output is correct |
2 |
Correct |
14 ms |
24832 KB |
Output is correct |
3 |
Correct |
15 ms |
24832 KB |
Output is correct |
4 |
Correct |
15 ms |
24832 KB |
Output is correct |
5 |
Correct |
15 ms |
24832 KB |
Output is correct |
6 |
Correct |
15 ms |
24832 KB |
Output is correct |
7 |
Correct |
14 ms |
24844 KB |
Output is correct |
8 |
Correct |
16 ms |
24832 KB |
Output is correct |
9 |
Correct |
15 ms |
24832 KB |
Output is correct |
10 |
Correct |
15 ms |
24832 KB |
Output is correct |
11 |
Correct |
15 ms |
24832 KB |
Output is correct |
12 |
Correct |
14 ms |
24960 KB |
Output is correct |
13 |
Correct |
15 ms |
24832 KB |
Output is correct |
14 |
Correct |
16 ms |
24832 KB |
Output is correct |
15 |
Correct |
17 ms |
24832 KB |
Output is correct |
16 |
Correct |
18 ms |
24832 KB |
Output is correct |
17 |
Correct |
15 ms |
24832 KB |
Output is correct |
18 |
Correct |
15 ms |
24832 KB |
Output is correct |
19 |
Correct |
14 ms |
24832 KB |
Output is correct |
20 |
Correct |
16 ms |
24832 KB |
Output is correct |
21 |
Correct |
17 ms |
24832 KB |
Output is correct |
22 |
Correct |
15 ms |
24832 KB |
Output is correct |
23 |
Correct |
15 ms |
24832 KB |
Output is correct |
24 |
Correct |
15 ms |
24832 KB |
Output is correct |
25 |
Correct |
15 ms |
24832 KB |
Output is correct |
26 |
Correct |
15 ms |
24832 KB |
Output is correct |
27 |
Correct |
15 ms |
24832 KB |
Output is correct |
28 |
Correct |
15 ms |
24832 KB |
Output is correct |
29 |
Correct |
15 ms |
24832 KB |
Output is correct |
30 |
Correct |
410 ms |
48680 KB |
Output is correct |
31 |
Correct |
429 ms |
49248 KB |
Output is correct |
32 |
Correct |
219 ms |
85624 KB |
Output is correct |
33 |
Correct |
232 ms |
75872 KB |
Output is correct |
34 |
Correct |
236 ms |
75768 KB |
Output is correct |
35 |
Correct |
253 ms |
71544 KB |
Output is correct |
36 |
Correct |
221 ms |
64120 KB |
Output is correct |
37 |
Correct |
206 ms |
60284 KB |
Output is correct |
38 |
Correct |
194 ms |
58744 KB |
Output is correct |
39 |
Correct |
196 ms |
57852 KB |
Output is correct |
40 |
Correct |
202 ms |
52084 KB |
Output is correct |
41 |
Correct |
264 ms |
49456 KB |
Output is correct |
42 |
Correct |
244 ms |
49788 KB |
Output is correct |
43 |
Correct |
271 ms |
76408 KB |
Output is correct |
44 |
Incorrect |
15 ms |
24832 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3586' |
45 |
Halted |
0 ms |
0 KB |
- |