#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long ll;
long long minimum_walk(std::vector<int> p, int s)
{
int n=p.size();
ll ans=0;
int start_l=0;
int start_r=n-1;
while(start_l<s && p[start_l]==start_l)
{
start_l++;
}
while(s<start_r && p[start_r]==start_r)
{
start_r--;
}
int mex_low=start_l;
int mex_high=start_r;
vector <bool> v(1000007,0);
int left_limit=start_l;
int right_limit=start_r;
for(int i=start_l;i<=s;i++)
{
ans+=abs(i-p[i]);
v[p[i]]=1;
while(v[mex_low]==1)
{
mex_low++;
}
if(mex_low>i)
{
if(i!=s)
ans+=2;
left_limit=i+1;
}
}
v=vector <bool> (1000007,0);
for(int i=start_r;i>=s;i--)
{
ans+=abs(i-p[i]);
v[p[i]]=1;
while(mex_high>=0 && v[mex_high]==1)
{
mex_high--;
}
if(mex_high<i)
{
if(i!=s)
ans+=2;
right_limit=i-1;
}
}
ans-=abs(s-p[s]);
int lptr=s;
int rptr=s;
v=vector <bool> (1000007,0);
mex_low=s;
mex_high=s;
int mini=p[s];
int maxi=p[s];
v[p[s]]=1;
while(lptr>=left_limit && rptr<=right_limit)
{
while(mini<lptr || rptr<maxi)
{
while(mini<lptr)
{
lptr--;
v[p[lptr]]=1;
mini=min(mini,p[lptr]);
maxi=max(maxi,p[lptr]);
}
while(rptr<maxi)
{
rptr++;
v[p[rptr]]=1;
mini=min(mini,p[rptr]);
maxi=max(maxi,p[rptr]);
}
}
while(mex_low>=0 && v[mex_low]==1)
{
mex_low--;
}
while(v[mex_high]==1)
{
mex_high++;
}
if(lptr==left_limit && rptr==right_limit)
{
break;
}
ans+=2;
mini--;
maxi++;
}
/*if(left_limit>right_limit)
{
ans-=2;
}*/
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
424 KB |
Output is correct |
4 |
Correct |
1 ms |
436 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
480 KB |
Output is correct |
15 |
Correct |
1 ms |
428 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
424 KB |
Output is correct |
4 |
Correct |
1 ms |
436 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
480 KB |
Output is correct |
15 |
Correct |
1 ms |
428 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
520 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
428 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
424 KB |
Output is correct |
4 |
Correct |
1 ms |
436 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
480 KB |
Output is correct |
15 |
Correct |
1 ms |
428 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
520 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
428 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
102 ms |
13004 KB |
Output is correct |
31 |
Correct |
92 ms |
12992 KB |
Output is correct |
32 |
Correct |
96 ms |
13084 KB |
Output is correct |
33 |
Correct |
103 ms |
12972 KB |
Output is correct |
34 |
Correct |
100 ms |
13100 KB |
Output is correct |
35 |
Correct |
94 ms |
12976 KB |
Output is correct |
36 |
Correct |
96 ms |
12992 KB |
Output is correct |
37 |
Correct |
98 ms |
13044 KB |
Output is correct |
38 |
Correct |
97 ms |
13104 KB |
Output is correct |
39 |
Correct |
101 ms |
13016 KB |
Output is correct |
40 |
Correct |
88 ms |
13072 KB |
Output is correct |
41 |
Correct |
132 ms |
13024 KB |
Output is correct |
42 |
Correct |
96 ms |
13112 KB |
Output is correct |
43 |
Correct |
92 ms |
13004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
428 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
436 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
424 KB |
Output is correct |
4 |
Correct |
1 ms |
436 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
480 KB |
Output is correct |
15 |
Correct |
1 ms |
428 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
520 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
428 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
102 ms |
13004 KB |
Output is correct |
31 |
Correct |
92 ms |
12992 KB |
Output is correct |
32 |
Correct |
96 ms |
13084 KB |
Output is correct |
33 |
Correct |
103 ms |
12972 KB |
Output is correct |
34 |
Correct |
100 ms |
13100 KB |
Output is correct |
35 |
Correct |
94 ms |
12976 KB |
Output is correct |
36 |
Correct |
96 ms |
12992 KB |
Output is correct |
37 |
Correct |
98 ms |
13044 KB |
Output is correct |
38 |
Correct |
97 ms |
13104 KB |
Output is correct |
39 |
Correct |
101 ms |
13016 KB |
Output is correct |
40 |
Correct |
88 ms |
13072 KB |
Output is correct |
41 |
Correct |
132 ms |
13024 KB |
Output is correct |
42 |
Correct |
96 ms |
13112 KB |
Output is correct |
43 |
Correct |
92 ms |
13004 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
428 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
0 ms |
468 KB |
Output is correct |
51 |
Correct |
1 ms |
468 KB |
Output is correct |
52 |
Correct |
1 ms |
436 KB |
Output is correct |
53 |
Correct |
1 ms |
468 KB |
Output is correct |
54 |
Correct |
0 ms |
468 KB |
Output is correct |
55 |
Correct |
1 ms |
468 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
0 ms |
468 KB |
Output is correct |
58 |
Correct |
0 ms |
468 KB |
Output is correct |
59 |
Correct |
1 ms |
468 KB |
Output is correct |
60 |
Correct |
1 ms |
468 KB |
Output is correct |
61 |
Correct |
1 ms |
468 KB |
Output is correct |
62 |
Correct |
1 ms |
468 KB |
Output is correct |
63 |
Correct |
1 ms |
468 KB |
Output is correct |
64 |
Correct |
91 ms |
15080 KB |
Output is correct |
65 |
Correct |
104 ms |
15080 KB |
Output is correct |
66 |
Correct |
95 ms |
15084 KB |
Output is correct |
67 |
Correct |
93 ms |
15080 KB |
Output is correct |
68 |
Correct |
9 ms |
1876 KB |
Output is correct |
69 |
Correct |
9 ms |
1852 KB |
Output is correct |
70 |
Correct |
9 ms |
1792 KB |
Output is correct |
71 |
Correct |
9 ms |
1852 KB |
Output is correct |
72 |
Correct |
10 ms |
1792 KB |
Output is correct |
73 |
Correct |
9 ms |
1788 KB |
Output is correct |
74 |
Correct |
92 ms |
14988 KB |
Output is correct |
75 |
Correct |
111 ms |
15200 KB |
Output is correct |
76 |
Correct |
97 ms |
15128 KB |
Output is correct |
77 |
Correct |
88 ms |
15104 KB |
Output is correct |
78 |
Correct |
92 ms |
15084 KB |
Output is correct |
79 |
Correct |
95 ms |
15088 KB |
Output is correct |
80 |
Correct |
94 ms |
15076 KB |
Output is correct |
81 |
Correct |
94 ms |
15072 KB |
Output is correct |
82 |
Correct |
90 ms |
15088 KB |
Output is correct |
83 |
Correct |
104 ms |
15084 KB |
Output is correct |
84 |
Correct |
106 ms |
15200 KB |
Output is correct |
85 |
Correct |
104 ms |
15092 KB |
Output is correct |
86 |
Correct |
90 ms |
15072 KB |
Output is correct |
87 |
Correct |
91 ms |
15068 KB |
Output is correct |
88 |
Correct |
93 ms |
15084 KB |
Output is correct |
89 |
Correct |
104 ms |
15092 KB |
Output is correct |
90 |
Correct |
100 ms |
15080 KB |
Output is correct |
91 |
Correct |
89 ms |
15076 KB |
Output is correct |
92 |
Correct |
90 ms |
15080 KB |
Output is correct |