#include "books.h"
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
typedef long long int ll;
typedef pair<int,int> pii;
const int MAXN = 1e6+1;
int mn[MAXN],mx[MAXN];
ll ans = 0;
int n = 0;
vector<int>p;
void extend(int &l,int &r){
int l0 = min(mn[l],mn[r]);
int r0 = max(mx[l],mx[r]);
while(l0 < l || r < r0){
while(l0 < l){
l--;
l0 = min(l0,mn[l]);
r0 = max(r0,mx[l]);
}
while(r0 > r){
r++;
l0 = min(l0,mn[r]);
r0 = max(r0,mx[r]);
}
}
}
void solve(int l,int r){
extend(l,r);
//cout<<l<<" "<<r<<'\n';
int rcost = 0;
int lcost = 0;
int l0 = l,r0 = r;
int L = l,R = r;
r++;
while(r < n){
rcost += 2;
extend(l,r);
if(l < l0)break;
r++;
}
L = l;
R = r;
l = l0,r = r0;
l--;
while(l >= 0){
lcost+=2;
extend(l,r);
if(r > r0)break;
l--;
}
L = min(L,l);
R = max(R,r);
//cout<<L<<" "<<R<<" "<<lcost<<" "<<rcost<<'\n';
if(R == n){ //end of the line
ans += rcost;
ans += lcost;
return;
}
ans += min(lcost,rcost);
solve(L,R);
}
ll minimum_walk(vector<int>P, int s) {
p = P;
n = sz(p);
for(int i=0;i<n;i++)ans+=abs(i-p[i]);
vector<bool>vis(n,0);
for(int i=0;i<n;i++){
if(vis[i])continue;
int cur = i;
mn[i] = mx[i] = i;
vector<int>path;
while(!vis[cur]){
vis[cur] = 1;
path.push_back(cur);
cur = p[cur];
}
for(int x:path){
mn[i] = min(mn[i],x);
mx[i] = max(mx[i],x);
}
for(int x:path){
mn[x] = mn[i];
mx[x] = mx[i];
}
}
// for(int i=0;i<n;i++)cout<<mn[i]<<" ";
// cout<<'\n';
// for(int i=0;i<n;i++)cout<<mx[i]<<" ";
// cout<<'\n';
solve(s,s);
for(int i=0;i<s;i++){
if(i==p[i])ans-=2;
else break;
}
for(int i=n-1;i>s;i--){
if(i==p[i])ans-=2;
else break;
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2392 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2392 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2392 KB |
Output is correct |
30 |
Correct |
169 ms |
30272 KB |
Output is correct |
31 |
Correct |
116 ms |
28676 KB |
Output is correct |
32 |
Correct |
116 ms |
26704 KB |
Output is correct |
33 |
Correct |
107 ms |
26596 KB |
Output is correct |
34 |
Correct |
110 ms |
26964 KB |
Output is correct |
35 |
Correct |
106 ms |
26704 KB |
Output is correct |
36 |
Correct |
133 ms |
26704 KB |
Output is correct |
37 |
Correct |
92 ms |
27112 KB |
Output is correct |
38 |
Correct |
99 ms |
26892 KB |
Output is correct |
39 |
Correct |
86 ms |
26704 KB |
Output is correct |
40 |
Correct |
88 ms |
27032 KB |
Output is correct |
41 |
Correct |
105 ms |
28208 KB |
Output is correct |
42 |
Correct |
95 ms |
27692 KB |
Output is correct |
43 |
Correct |
101 ms |
26780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2392 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2392 KB |
Output is correct |
30 |
Correct |
169 ms |
30272 KB |
Output is correct |
31 |
Correct |
116 ms |
28676 KB |
Output is correct |
32 |
Correct |
116 ms |
26704 KB |
Output is correct |
33 |
Correct |
107 ms |
26596 KB |
Output is correct |
34 |
Correct |
110 ms |
26964 KB |
Output is correct |
35 |
Correct |
106 ms |
26704 KB |
Output is correct |
36 |
Correct |
133 ms |
26704 KB |
Output is correct |
37 |
Correct |
92 ms |
27112 KB |
Output is correct |
38 |
Correct |
99 ms |
26892 KB |
Output is correct |
39 |
Correct |
86 ms |
26704 KB |
Output is correct |
40 |
Correct |
88 ms |
27032 KB |
Output is correct |
41 |
Correct |
105 ms |
28208 KB |
Output is correct |
42 |
Correct |
95 ms |
27692 KB |
Output is correct |
43 |
Correct |
101 ms |
26780 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
1 ms |
2396 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
1 ms |
2396 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
1 ms |
2396 KB |
Output is correct |
50 |
Correct |
1 ms |
2396 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
1 ms |
2396 KB |
Output is correct |
53 |
Correct |
1 ms |
2396 KB |
Output is correct |
54 |
Correct |
1 ms |
2396 KB |
Output is correct |
55 |
Correct |
1 ms |
2396 KB |
Output is correct |
56 |
Correct |
1 ms |
2396 KB |
Output is correct |
57 |
Correct |
1 ms |
2396 KB |
Output is correct |
58 |
Correct |
1 ms |
2396 KB |
Output is correct |
59 |
Correct |
1 ms |
2396 KB |
Output is correct |
60 |
Correct |
1 ms |
2396 KB |
Output is correct |
61 |
Correct |
1 ms |
2396 KB |
Output is correct |
62 |
Correct |
1 ms |
2396 KB |
Output is correct |
63 |
Correct |
1 ms |
2396 KB |
Output is correct |
64 |
Correct |
103 ms |
26712 KB |
Output is correct |
65 |
Correct |
139 ms |
26616 KB |
Output is correct |
66 |
Correct |
91 ms |
26868 KB |
Output is correct |
67 |
Correct |
85 ms |
26704 KB |
Output is correct |
68 |
Correct |
10 ms |
8540 KB |
Output is correct |
69 |
Correct |
10 ms |
8540 KB |
Output is correct |
70 |
Correct |
10 ms |
8384 KB |
Output is correct |
71 |
Correct |
17 ms |
8540 KB |
Output is correct |
72 |
Correct |
17 ms |
8536 KB |
Output is correct |
73 |
Correct |
9 ms |
8540 KB |
Output is correct |
74 |
Correct |
103 ms |
26612 KB |
Output is correct |
75 |
Correct |
104 ms |
26704 KB |
Output is correct |
76 |
Correct |
104 ms |
26872 KB |
Output is correct |
77 |
Correct |
100 ms |
26932 KB |
Output is correct |
78 |
Correct |
109 ms |
26616 KB |
Output is correct |
79 |
Correct |
111 ms |
26632 KB |
Output is correct |
80 |
Correct |
104 ms |
26652 KB |
Output is correct |
81 |
Correct |
118 ms |
52720 KB |
Output is correct |
82 |
Correct |
141 ms |
52820 KB |
Output is correct |
83 |
Correct |
104 ms |
29168 KB |
Output is correct |
84 |
Correct |
103 ms |
28408 KB |
Output is correct |
85 |
Correct |
95 ms |
27376 KB |
Output is correct |
86 |
Correct |
92 ms |
27008 KB |
Output is correct |
87 |
Correct |
129 ms |
38768 KB |
Output is correct |
88 |
Correct |
150 ms |
33780 KB |
Output is correct |
89 |
Correct |
129 ms |
29812 KB |
Output is correct |
90 |
Correct |
87 ms |
26876 KB |
Output is correct |
91 |
Correct |
112 ms |
26708 KB |
Output is correct |
92 |
Correct |
89 ms |
26960 KB |
Output is correct |