#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MN = 1e6+6;
ll n, s, x, id[MN], l[MN], r[MN], a[MN], b[MN], aa[MN], bb[MN], nxt, ans, arr[MN], i, j, par[MN], dist[MN], L=1<<30, R, vs[MN];
vector<int> adj[MN], rev[MN];
deque<int> q;
void dfs(ll n){
ans += abs(arr[n]-n);
a[id[n]]=min(a[id[n]],n);
b[id[n]]=max(b[id[n]],n);
if(n<=s) aa[id[n]]=max(aa[id[n]],n);
if(n>=s) bb[id[n]]=min(bb[id[n]],n);
if(id[arr[n]]) return;
id[arr[n]]=id[n];
dfs(arr[n]);
}
ll minimum_walk(vector<int> p,int e){
n = p.size(); s = e+1;
for(i=0;i<n;i++) arr[i+1]=p[i]+1;
for(i=1;i<=n;i++){
a[i] = bb[i] = 1<<30;
if(!id[i]){
id[i] = ++nxt;
dfs(i);
}
}
for(i=1;i<=nxt;i++){
if(a[i]==b[i]) continue;
L = min(L, a[i]);
R = max(R, b[i]);
if(a[i]<=s&&s<=b[i]){
l[a[i]]++; l[b[i]]--;
l[aa[i]]--; l[s]++;
r[b[i]]++; r[a[i]]--;
r[s]++; r[bb[i]]--;
adj[aa[i]].push_back(bb[i]);
adj[bb[i]].push_back(aa[i]);
}
else{
l[a[i]]++; l[b[i]]--;
r[b[i]]++; r[a[i]]--;
}
}
if(R==0) return 0LL;
for(i=1;i<=n;i++){
x += l[i];
if(x) adj[i+1].push_back(i);
}
x = 0;
for(i=n;i>=1;i--){
x += r[i];
if(x) adj[i-1].push_back(i);
}
memset(dist,-1,sizeof(dist));
q.push_back(s); dist[s] = 0;
while(q.size()){
x = q.front(); q.pop_front();
if(vs[x]) continue;
vs[x] = 1;
for(auto v : adj[x]){
if(dist[v]==-1||dist[x]<dist[v]){
dist[v] = dist[x]; par[v] = x;
q.push_front(v);
}
}
if(x>1&&dist[x-1]==-1){
dist[x-1] = dist[x]+1; par[x-1] = x;
q.push_back(x-1);
}
if(x<n&&dist[x+1]==-1){
dist[x+1] = dist[x]+1; par[x+1] = x;
q.push_back(x+1);
}
}
ans += 2*dist[L];
memset(dist,-1,sizeof(dist));
memset(vs,0,sizeof(vs));
x = L;
while(1){
q.push_back(x);
dist[x] = 0;
if(!par[x]) break;
x = par[x];
}
while(q.size()){
x = q.front(); q.pop_front();
if(vs[x]) continue;
vs[x] = 1;
for(auto v : adj[x]){
if(dist[v]==-1||dist[x]<dist[v]){
dist[v] = dist[x]; par[v] = x;
q.push_front(v);
}
}
if(x>1&&dist[x-1]==-1){
dist[x-1] = dist[x]+1; par[x-1] = x;
q.push_back(x-1);
}
if(x<n&&dist[x+1]==-1){
dist[x+1] = dist[x]+1; par[x+1] = x;
q.push_back(x+1);
}
}
ans += 2*dist[R];
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
62968 KB |
Output is correct |
2 |
Correct |
55 ms |
62968 KB |
Output is correct |
3 |
Correct |
55 ms |
63056 KB |
Output is correct |
4 |
Correct |
55 ms |
62972 KB |
Output is correct |
5 |
Correct |
55 ms |
62968 KB |
Output is correct |
6 |
Correct |
56 ms |
62968 KB |
Output is correct |
7 |
Correct |
56 ms |
63096 KB |
Output is correct |
8 |
Correct |
55 ms |
62968 KB |
Output is correct |
9 |
Correct |
42 ms |
47316 KB |
Output is correct |
10 |
Correct |
56 ms |
63096 KB |
Output is correct |
11 |
Correct |
42 ms |
47352 KB |
Output is correct |
12 |
Correct |
53 ms |
62968 KB |
Output is correct |
13 |
Correct |
55 ms |
62968 KB |
Output is correct |
14 |
Correct |
41 ms |
47356 KB |
Output is correct |
15 |
Correct |
54 ms |
62968 KB |
Output is correct |
16 |
Correct |
55 ms |
62968 KB |
Output is correct |
17 |
Correct |
54 ms |
62968 KB |
Output is correct |
18 |
Correct |
55 ms |
62968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
62968 KB |
Output is correct |
2 |
Correct |
55 ms |
62968 KB |
Output is correct |
3 |
Correct |
55 ms |
63056 KB |
Output is correct |
4 |
Correct |
55 ms |
62972 KB |
Output is correct |
5 |
Correct |
55 ms |
62968 KB |
Output is correct |
6 |
Correct |
56 ms |
62968 KB |
Output is correct |
7 |
Correct |
56 ms |
63096 KB |
Output is correct |
8 |
Correct |
55 ms |
62968 KB |
Output is correct |
9 |
Correct |
42 ms |
47316 KB |
Output is correct |
10 |
Correct |
56 ms |
63096 KB |
Output is correct |
11 |
Correct |
42 ms |
47352 KB |
Output is correct |
12 |
Correct |
53 ms |
62968 KB |
Output is correct |
13 |
Correct |
55 ms |
62968 KB |
Output is correct |
14 |
Correct |
41 ms |
47356 KB |
Output is correct |
15 |
Correct |
54 ms |
62968 KB |
Output is correct |
16 |
Correct |
55 ms |
62968 KB |
Output is correct |
17 |
Correct |
54 ms |
62968 KB |
Output is correct |
18 |
Correct |
55 ms |
62968 KB |
Output is correct |
19 |
Correct |
54 ms |
63096 KB |
Output is correct |
20 |
Correct |
54 ms |
63124 KB |
Output is correct |
21 |
Correct |
42 ms |
47352 KB |
Output is correct |
22 |
Correct |
54 ms |
63096 KB |
Output is correct |
23 |
Correct |
55 ms |
63224 KB |
Output is correct |
24 |
Correct |
56 ms |
63096 KB |
Output is correct |
25 |
Correct |
55 ms |
63096 KB |
Output is correct |
26 |
Correct |
55 ms |
63096 KB |
Output is correct |
27 |
Correct |
53 ms |
63096 KB |
Output is correct |
28 |
Correct |
54 ms |
63224 KB |
Output is correct |
29 |
Correct |
54 ms |
63096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
62968 KB |
Output is correct |
2 |
Correct |
55 ms |
62968 KB |
Output is correct |
3 |
Correct |
55 ms |
63056 KB |
Output is correct |
4 |
Correct |
55 ms |
62972 KB |
Output is correct |
5 |
Correct |
55 ms |
62968 KB |
Output is correct |
6 |
Correct |
56 ms |
62968 KB |
Output is correct |
7 |
Correct |
56 ms |
63096 KB |
Output is correct |
8 |
Correct |
55 ms |
62968 KB |
Output is correct |
9 |
Correct |
42 ms |
47316 KB |
Output is correct |
10 |
Correct |
56 ms |
63096 KB |
Output is correct |
11 |
Correct |
42 ms |
47352 KB |
Output is correct |
12 |
Correct |
53 ms |
62968 KB |
Output is correct |
13 |
Correct |
55 ms |
62968 KB |
Output is correct |
14 |
Correct |
41 ms |
47356 KB |
Output is correct |
15 |
Correct |
54 ms |
62968 KB |
Output is correct |
16 |
Correct |
55 ms |
62968 KB |
Output is correct |
17 |
Correct |
54 ms |
62968 KB |
Output is correct |
18 |
Correct |
55 ms |
62968 KB |
Output is correct |
19 |
Correct |
54 ms |
63096 KB |
Output is correct |
20 |
Correct |
54 ms |
63124 KB |
Output is correct |
21 |
Correct |
42 ms |
47352 KB |
Output is correct |
22 |
Correct |
54 ms |
63096 KB |
Output is correct |
23 |
Correct |
55 ms |
63224 KB |
Output is correct |
24 |
Correct |
56 ms |
63096 KB |
Output is correct |
25 |
Correct |
55 ms |
63096 KB |
Output is correct |
26 |
Correct |
55 ms |
63096 KB |
Output is correct |
27 |
Correct |
53 ms |
63096 KB |
Output is correct |
28 |
Correct |
54 ms |
63224 KB |
Output is correct |
29 |
Correct |
54 ms |
63096 KB |
Output is correct |
30 |
Correct |
470 ms |
141688 KB |
Output is correct |
31 |
Correct |
483 ms |
141688 KB |
Output is correct |
32 |
Correct |
190 ms |
94408 KB |
Output is correct |
33 |
Correct |
321 ms |
148028 KB |
Output is correct |
34 |
Correct |
311 ms |
148088 KB |
Output is correct |
35 |
Correct |
329 ms |
152824 KB |
Output is correct |
36 |
Correct |
349 ms |
157688 KB |
Output is correct |
37 |
Correct |
353 ms |
157304 KB |
Output is correct |
38 |
Correct |
355 ms |
153052 KB |
Output is correct |
39 |
Correct |
354 ms |
151712 KB |
Output is correct |
40 |
Correct |
362 ms |
144888 KB |
Output is correct |
41 |
Correct |
392 ms |
141944 KB |
Output is correct |
42 |
Correct |
375 ms |
142328 KB |
Output is correct |
43 |
Correct |
374 ms |
163040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
63096 KB |
Output is correct |
2 |
Correct |
53 ms |
63184 KB |
Output is correct |
3 |
Correct |
55 ms |
63096 KB |
Output is correct |
4 |
Correct |
56 ms |
63096 KB |
Output is correct |
5 |
Correct |
57 ms |
63224 KB |
Output is correct |
6 |
Correct |
56 ms |
63096 KB |
Output is correct |
7 |
Correct |
56 ms |
63124 KB |
Output is correct |
8 |
Correct |
55 ms |
63096 KB |
Output is correct |
9 |
Correct |
57 ms |
63096 KB |
Output is correct |
10 |
Correct |
44 ms |
47352 KB |
Output is correct |
11 |
Correct |
62 ms |
63096 KB |
Output is correct |
12 |
Correct |
56 ms |
63068 KB |
Output is correct |
13 |
Correct |
83 ms |
63152 KB |
Output is correct |
14 |
Correct |
57 ms |
63144 KB |
Output is correct |
15 |
Correct |
55 ms |
63096 KB |
Output is correct |
16 |
Correct |
56 ms |
63096 KB |
Output is correct |
17 |
Correct |
56 ms |
63100 KB |
Output is correct |
18 |
Correct |
55 ms |
63096 KB |
Output is correct |
19 |
Correct |
55 ms |
63096 KB |
Output is correct |
20 |
Correct |
66 ms |
63096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
62968 KB |
Output is correct |
2 |
Correct |
55 ms |
62968 KB |
Output is correct |
3 |
Correct |
55 ms |
63056 KB |
Output is correct |
4 |
Correct |
55 ms |
62972 KB |
Output is correct |
5 |
Correct |
55 ms |
62968 KB |
Output is correct |
6 |
Correct |
56 ms |
62968 KB |
Output is correct |
7 |
Correct |
56 ms |
63096 KB |
Output is correct |
8 |
Correct |
55 ms |
62968 KB |
Output is correct |
9 |
Correct |
42 ms |
47316 KB |
Output is correct |
10 |
Correct |
56 ms |
63096 KB |
Output is correct |
11 |
Correct |
42 ms |
47352 KB |
Output is correct |
12 |
Correct |
53 ms |
62968 KB |
Output is correct |
13 |
Correct |
55 ms |
62968 KB |
Output is correct |
14 |
Correct |
41 ms |
47356 KB |
Output is correct |
15 |
Correct |
54 ms |
62968 KB |
Output is correct |
16 |
Correct |
55 ms |
62968 KB |
Output is correct |
17 |
Correct |
54 ms |
62968 KB |
Output is correct |
18 |
Correct |
55 ms |
62968 KB |
Output is correct |
19 |
Correct |
54 ms |
63096 KB |
Output is correct |
20 |
Correct |
54 ms |
63124 KB |
Output is correct |
21 |
Correct |
42 ms |
47352 KB |
Output is correct |
22 |
Correct |
54 ms |
63096 KB |
Output is correct |
23 |
Correct |
55 ms |
63224 KB |
Output is correct |
24 |
Correct |
56 ms |
63096 KB |
Output is correct |
25 |
Correct |
55 ms |
63096 KB |
Output is correct |
26 |
Correct |
55 ms |
63096 KB |
Output is correct |
27 |
Correct |
53 ms |
63096 KB |
Output is correct |
28 |
Correct |
54 ms |
63224 KB |
Output is correct |
29 |
Correct |
54 ms |
63096 KB |
Output is correct |
30 |
Correct |
470 ms |
141688 KB |
Output is correct |
31 |
Correct |
483 ms |
141688 KB |
Output is correct |
32 |
Correct |
190 ms |
94408 KB |
Output is correct |
33 |
Correct |
321 ms |
148028 KB |
Output is correct |
34 |
Correct |
311 ms |
148088 KB |
Output is correct |
35 |
Correct |
329 ms |
152824 KB |
Output is correct |
36 |
Correct |
349 ms |
157688 KB |
Output is correct |
37 |
Correct |
353 ms |
157304 KB |
Output is correct |
38 |
Correct |
355 ms |
153052 KB |
Output is correct |
39 |
Correct |
354 ms |
151712 KB |
Output is correct |
40 |
Correct |
362 ms |
144888 KB |
Output is correct |
41 |
Correct |
392 ms |
141944 KB |
Output is correct |
42 |
Correct |
375 ms |
142328 KB |
Output is correct |
43 |
Correct |
374 ms |
163040 KB |
Output is correct |
44 |
Correct |
54 ms |
63096 KB |
Output is correct |
45 |
Correct |
53 ms |
63184 KB |
Output is correct |
46 |
Correct |
55 ms |
63096 KB |
Output is correct |
47 |
Correct |
56 ms |
63096 KB |
Output is correct |
48 |
Correct |
57 ms |
63224 KB |
Output is correct |
49 |
Correct |
56 ms |
63096 KB |
Output is correct |
50 |
Correct |
56 ms |
63124 KB |
Output is correct |
51 |
Correct |
55 ms |
63096 KB |
Output is correct |
52 |
Correct |
57 ms |
63096 KB |
Output is correct |
53 |
Correct |
44 ms |
47352 KB |
Output is correct |
54 |
Correct |
62 ms |
63096 KB |
Output is correct |
55 |
Correct |
56 ms |
63068 KB |
Output is correct |
56 |
Correct |
83 ms |
63152 KB |
Output is correct |
57 |
Correct |
57 ms |
63144 KB |
Output is correct |
58 |
Correct |
55 ms |
63096 KB |
Output is correct |
59 |
Correct |
56 ms |
63096 KB |
Output is correct |
60 |
Correct |
56 ms |
63100 KB |
Output is correct |
61 |
Correct |
55 ms |
63096 KB |
Output is correct |
62 |
Correct |
55 ms |
63096 KB |
Output is correct |
63 |
Correct |
66 ms |
63096 KB |
Output is correct |
64 |
Correct |
385 ms |
170328 KB |
Output is correct |
65 |
Correct |
371 ms |
176172 KB |
Output is correct |
66 |
Correct |
351 ms |
166216 KB |
Output is correct |
67 |
Correct |
352 ms |
165368 KB |
Output is correct |
68 |
Correct |
85 ms |
73356 KB |
Output is correct |
69 |
Correct |
84 ms |
73208 KB |
Output is correct |
70 |
Correct |
84 ms |
73592 KB |
Output is correct |
71 |
Correct |
83 ms |
73592 KB |
Output is correct |
72 |
Correct |
84 ms |
74616 KB |
Output is correct |
73 |
Correct |
84 ms |
72056 KB |
Output is correct |
74 |
Correct |
321 ms |
160376 KB |
Output is correct |
75 |
Correct |
318 ms |
159384 KB |
Output is correct |
76 |
Correct |
358 ms |
173304 KB |
Output is correct |
77 |
Correct |
371 ms |
174328 KB |
Output is correct |
78 |
Correct |
380 ms |
172384 KB |
Output is correct |
79 |
Correct |
376 ms |
174712 KB |
Output is correct |
80 |
Correct |
207 ms |
108920 KB |
Output is correct |
81 |
Correct |
377 ms |
172528 KB |
Output is correct |
82 |
Correct |
369 ms |
172928 KB |
Output is correct |
83 |
Correct |
394 ms |
169404 KB |
Output is correct |
84 |
Correct |
373 ms |
168512 KB |
Output is correct |
85 |
Correct |
382 ms |
166308 KB |
Output is correct |
86 |
Correct |
365 ms |
165276 KB |
Output is correct |
87 |
Correct |
370 ms |
175864 KB |
Output is correct |
88 |
Correct |
381 ms |
173704 KB |
Output is correct |
89 |
Correct |
401 ms |
170488 KB |
Output is correct |
90 |
Correct |
384 ms |
164872 KB |
Output is correct |
91 |
Correct |
377 ms |
162968 KB |
Output is correct |
92 |
Correct |
375 ms |
153368 KB |
Output is correct |