#ifndef LOCAL
#include "books.h"
#endif
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define pb push_back
#define mp make_pair
#define taskname "A"
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef tree <int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size();
int L = 0, R = n - 1;
while (L < s && p[L] == L)++L;
while (R > s && p[R] == R)--R;
ll res = 0;
vector<int> mn(n , -1);
vector<int> mx(n , -1);
for(int i = 0 ; i < n ; ++i){
res += abs(i - p[i]);
if(mn[i] != -1)continue;
int j = i;
while(true){
mn[j] = i;
mx[i] = max(mx[i] , j);
if(p[j] == i)break;
j = p[j];
}
j = i;
while(true){
mx[j] = mx[i];
if(p[j] == i)break;
j = p[j];
}
}
int valL = 0, valR = 0;
for (int i = L, tmp = 0; i < s; ++i)
{
tmp = max(tmp , mx[i]);
if(tmp == i)valL += 2;
}
for (int i = R, tmp = n; i > s; --i)
{
tmp = min(tmp, mn[i]);
if(tmp == i)valR += 2;
}
res += min(valL , valR);
auto extend = [&](int & l , int & r , int ll , int rr){
while(ll < l || r < rr){
if(ll < l){
--l;
ll = min(ll , mn[l]);
rr = max(rr , mx[l]);
}
if(r < rr){
++r;
ll = min(ll , mn[r]);
rr = max(rr , mx[r]);
}
}
};
int l = s , r = s;
extend(l,r , mn[s] , mx[s]);
while(L < l || r < R){
res += 2;
extend(l,r,max(L , l - 1),min(R,r+1));
}
return res;
}
#ifdef LOCAL
#include "books.h"
#include <cstdio>
#include <vector>
#include <cassert>
using namespace std;
int main() {
freopen(taskname".INP","r",stdin);
int n, s;
assert(2 == scanf("%d %d", &n, &s));
vector<int> p((unsigned) n);
for(int i = 0; i < n; i++)
assert(1 == scanf("%d", &p[(unsigned) i]));
long long res = minimum_walk(p, s);
printf("%lld\n", res);
return 0;
}
#endif // LOCAL
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
368 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
368 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
372 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
368 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
372 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
318 ms |
22700 KB |
Output is correct |
31 |
Correct |
313 ms |
22776 KB |
Output is correct |
32 |
Correct |
145 ms |
22648 KB |
Output is correct |
33 |
Correct |
155 ms |
22648 KB |
Output is correct |
34 |
Correct |
167 ms |
22704 KB |
Output is correct |
35 |
Correct |
162 ms |
22708 KB |
Output is correct |
36 |
Correct |
164 ms |
22648 KB |
Output is correct |
37 |
Correct |
162 ms |
22648 KB |
Output is correct |
38 |
Correct |
168 ms |
22648 KB |
Output is correct |
39 |
Correct |
168 ms |
22648 KB |
Output is correct |
40 |
Correct |
179 ms |
22648 KB |
Output is correct |
41 |
Correct |
215 ms |
22776 KB |
Output is correct |
42 |
Correct |
212 ms |
22704 KB |
Output is correct |
43 |
Correct |
177 ms |
22776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
368 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
372 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
318 ms |
22700 KB |
Output is correct |
31 |
Correct |
313 ms |
22776 KB |
Output is correct |
32 |
Correct |
145 ms |
22648 KB |
Output is correct |
33 |
Correct |
155 ms |
22648 KB |
Output is correct |
34 |
Correct |
167 ms |
22704 KB |
Output is correct |
35 |
Correct |
162 ms |
22708 KB |
Output is correct |
36 |
Correct |
164 ms |
22648 KB |
Output is correct |
37 |
Correct |
162 ms |
22648 KB |
Output is correct |
38 |
Correct |
168 ms |
22648 KB |
Output is correct |
39 |
Correct |
168 ms |
22648 KB |
Output is correct |
40 |
Correct |
179 ms |
22648 KB |
Output is correct |
41 |
Correct |
215 ms |
22776 KB |
Output is correct |
42 |
Correct |
212 ms |
22704 KB |
Output is correct |
43 |
Correct |
177 ms |
22776 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
1 ms |
384 KB |
Output is correct |
49 |
Correct |
1 ms |
384 KB |
Output is correct |
50 |
Correct |
1 ms |
384 KB |
Output is correct |
51 |
Correct |
1 ms |
384 KB |
Output is correct |
52 |
Correct |
1 ms |
384 KB |
Output is correct |
53 |
Correct |
1 ms |
384 KB |
Output is correct |
54 |
Correct |
1 ms |
384 KB |
Output is correct |
55 |
Correct |
1 ms |
384 KB |
Output is correct |
56 |
Correct |
1 ms |
384 KB |
Output is correct |
57 |
Correct |
1 ms |
384 KB |
Output is correct |
58 |
Correct |
1 ms |
384 KB |
Output is correct |
59 |
Correct |
1 ms |
384 KB |
Output is correct |
60 |
Correct |
1 ms |
384 KB |
Output is correct |
61 |
Correct |
1 ms |
384 KB |
Output is correct |
62 |
Correct |
1 ms |
384 KB |
Output is correct |
63 |
Correct |
1 ms |
384 KB |
Output is correct |
64 |
Correct |
158 ms |
22776 KB |
Output is correct |
65 |
Correct |
150 ms |
22700 KB |
Output is correct |
66 |
Correct |
206 ms |
22648 KB |
Output is correct |
67 |
Correct |
171 ms |
22700 KB |
Output is correct |
68 |
Correct |
19 ms |
2432 KB |
Output is correct |
69 |
Correct |
16 ms |
2432 KB |
Output is correct |
70 |
Correct |
16 ms |
2432 KB |
Output is correct |
71 |
Correct |
15 ms |
2432 KB |
Output is correct |
72 |
Correct |
16 ms |
2432 KB |
Output is correct |
73 |
Correct |
17 ms |
2432 KB |
Output is correct |
74 |
Correct |
164 ms |
22648 KB |
Output is correct |
75 |
Correct |
159 ms |
22648 KB |
Output is correct |
76 |
Correct |
168 ms |
22648 KB |
Output is correct |
77 |
Correct |
185 ms |
22648 KB |
Output is correct |
78 |
Correct |
158 ms |
22652 KB |
Output is correct |
79 |
Correct |
156 ms |
22648 KB |
Output is correct |
80 |
Correct |
157 ms |
22648 KB |
Output is correct |
81 |
Correct |
148 ms |
22648 KB |
Output is correct |
82 |
Correct |
156 ms |
22652 KB |
Output is correct |
83 |
Correct |
157 ms |
22648 KB |
Output is correct |
84 |
Correct |
160 ms |
22776 KB |
Output is correct |
85 |
Correct |
182 ms |
22704 KB |
Output is correct |
86 |
Correct |
182 ms |
22648 KB |
Output is correct |
87 |
Correct |
162 ms |
22648 KB |
Output is correct |
88 |
Correct |
159 ms |
22648 KB |
Output is correct |
89 |
Correct |
193 ms |
22776 KB |
Output is correct |
90 |
Correct |
262 ms |
22648 KB |
Output is correct |
91 |
Correct |
166 ms |
22648 KB |
Output is correct |
92 |
Correct |
177 ms |
22776 KB |
Output is correct |