#include "books.h"
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define pll pair <ll,ll>
#define fi first
#define se second
#define MP make_pair
#define sz(a) (ll((a).size()))
#define BIT(mask,i) (((mask) >> (i))&1)
#define MASK(i) (1LL << (i))
const ll MAXN = 1e6+100;
ll cnt[MAXN];
bool in[MAXN];
pll rg[MAXN];
ll dist[MAXN];
long long minimum_walk(std::vector<int> p, int s) {
ll n = sz(p);
for (ll i = 0;i < sz(p);i ++){
if (p[i] > i){
cnt[i]++,cnt[p[i]]--;
}
}
for (ll i = 1;i < n;i ++)cnt[i] += cnt[i-1];
int l = n + 1,r = -1;
ll res=0;
for (int i = 0;i < n;i ++){
if (cnt[i]){
res += cnt[i] * 2 - 2;
l = min(l,i);
r = max(r,i+1);
}
}
l = min(l,s);
r = max(r,s);
res += (r-l)*2;
if (s&&cnt[s]&&cnt[s-1]){
for (ll i = 0;i < n; i ++){
if (!in[i]){
ll cur = i;
vector <ll> all;
while (!in[cur]){
all.push_back(cur);
in[cur] = 1;
cur = p[cur];
}
ll min1,max1;
min1=n+1,max1=-1;
for (auto x:all){
min1 = min(min1,x);
max1 = max(max1,x);
}
for (auto x:all)rg[x] = MP(min1,max1);
}
}
memset(in,0,sizeof in);
ll ptr = s;
while (cnt[ptr])ptr++;
vector <ll> q;
q.push_back(s);
set <ll> sus;
for (ll i = 0;i < n;i ++){
sus.insert(i);
}
sus.erase(s);
while (!q.empty()){
for (ll j = 0;j < sz(q);j ++){
ll x = q[j];
for (auto it = sus.lower_bound(rg[x].fi); it != sus.end() && (*it) <= rg[x].se;it = sus.erase(it)){
dist[*it] = dist[x];
q.push_back(*it);
}
}
vector <ll> tmp;
for (ll j = 0;j < sz(q);j ++){
ll x = q[j];
if (x + 1 < n && sus.find(x+1)!=sus.end()){
dist[x+1] = dist[x] + 1;
tmp.push_back(x+1);
sus.erase(x+1);
}
if (x-1>=0 && sus.find(x-1)!=sus.end()){
dist[x-1] = dist[x] + 1;
tmp.push_back(x-1);
sus.erase(x-1);
}
}
swap(q,tmp);
}
res+=dist[ptr]*2;
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
0 ms |
2396 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
0 ms |
2396 KB |
Output is correct |
26 |
Correct |
0 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 |
0 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
0 ms |
2396 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
0 ms |
2396 KB |
Output is correct |
26 |
Correct |
0 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 |
0 ms |
2392 KB |
Output is correct |
30 |
Correct |
69 ms |
17676 KB |
Output is correct |
31 |
Correct |
62 ms |
17748 KB |
Output is correct |
32 |
Correct |
64 ms |
15952 KB |
Output is correct |
33 |
Correct |
64 ms |
17656 KB |
Output is correct |
34 |
Correct |
69 ms |
17744 KB |
Output is correct |
35 |
Correct |
59 ms |
17744 KB |
Output is correct |
36 |
Correct |
57 ms |
17744 KB |
Output is correct |
37 |
Correct |
56 ms |
17748 KB |
Output is correct |
38 |
Correct |
56 ms |
17676 KB |
Output is correct |
39 |
Correct |
63 ms |
17748 KB |
Output is correct |
40 |
Correct |
57 ms |
17676 KB |
Output is correct |
41 |
Correct |
59 ms |
17676 KB |
Output is correct |
42 |
Correct |
59 ms |
17744 KB |
Output is correct |
43 |
Correct |
58 ms |
17680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 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 |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4696 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4544 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
0 ms |
2396 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
0 ms |
2396 KB |
Output is correct |
26 |
Correct |
0 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 |
0 ms |
2392 KB |
Output is correct |
30 |
Correct |
69 ms |
17676 KB |
Output is correct |
31 |
Correct |
62 ms |
17748 KB |
Output is correct |
32 |
Correct |
64 ms |
15952 KB |
Output is correct |
33 |
Correct |
64 ms |
17656 KB |
Output is correct |
34 |
Correct |
69 ms |
17744 KB |
Output is correct |
35 |
Correct |
59 ms |
17744 KB |
Output is correct |
36 |
Correct |
57 ms |
17744 KB |
Output is correct |
37 |
Correct |
56 ms |
17748 KB |
Output is correct |
38 |
Correct |
56 ms |
17676 KB |
Output is correct |
39 |
Correct |
63 ms |
17748 KB |
Output is correct |
40 |
Correct |
57 ms |
17676 KB |
Output is correct |
41 |
Correct |
59 ms |
17676 KB |
Output is correct |
42 |
Correct |
59 ms |
17744 KB |
Output is correct |
43 |
Correct |
58 ms |
17680 KB |
Output is correct |
44 |
Correct |
1 ms |
4440 KB |
Output is correct |
45 |
Correct |
1 ms |
4444 KB |
Output is correct |
46 |
Correct |
1 ms |
4444 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 |
4440 KB |
Output is correct |
50 |
Correct |
1 ms |
4444 KB |
Output is correct |
51 |
Correct |
1 ms |
4444 KB |
Output is correct |
52 |
Correct |
1 ms |
4444 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
4444 KB |
Output is correct |
55 |
Correct |
1 ms |
4696 KB |
Output is correct |
56 |
Correct |
1 ms |
4444 KB |
Output is correct |
57 |
Correct |
1 ms |
4444 KB |
Output is correct |
58 |
Correct |
1 ms |
4544 KB |
Output is correct |
59 |
Correct |
1 ms |
4440 KB |
Output is correct |
60 |
Correct |
1 ms |
4440 KB |
Output is correct |
61 |
Correct |
1 ms |
4444 KB |
Output is correct |
62 |
Correct |
1 ms |
4444 KB |
Output is correct |
63 |
Correct |
1 ms |
4444 KB |
Output is correct |
64 |
Correct |
308 ms |
94288 KB |
Output is correct |
65 |
Correct |
313 ms |
94036 KB |
Output is correct |
66 |
Correct |
355 ms |
94296 KB |
Output is correct |
67 |
Correct |
327 ms |
94080 KB |
Output is correct |
68 |
Correct |
25 ms |
16732 KB |
Output is correct |
69 |
Correct |
28 ms |
15444 KB |
Output is correct |
70 |
Correct |
28 ms |
15472 KB |
Output is correct |
71 |
Correct |
25 ms |
16788 KB |
Output is correct |
72 |
Correct |
26 ms |
15416 KB |
Output is correct |
73 |
Correct |
24 ms |
16860 KB |
Output is correct |
74 |
Correct |
67 ms |
24404 KB |
Output is correct |
75 |
Correct |
72 ms |
24404 KB |
Output is correct |
76 |
Correct |
298 ms |
94080 KB |
Output is correct |
77 |
Correct |
270 ms |
94208 KB |
Output is correct |
78 |
Correct |
369 ms |
94032 KB |
Output is correct |
79 |
Correct |
379 ms |
94032 KB |
Output is correct |
80 |
Correct |
88 ms |
22612 KB |
Output is correct |
81 |
Correct |
474 ms |
94032 KB |
Output is correct |
82 |
Correct |
490 ms |
94072 KB |
Output is correct |
83 |
Correct |
429 ms |
94032 KB |
Output is correct |
84 |
Correct |
464 ms |
94292 KB |
Output is correct |
85 |
Correct |
391 ms |
94068 KB |
Output is correct |
86 |
Correct |
395 ms |
94132 KB |
Output is correct |
87 |
Correct |
469 ms |
94288 KB |
Output is correct |
88 |
Correct |
471 ms |
94036 KB |
Output is correct |
89 |
Correct |
450 ms |
94168 KB |
Output is correct |
90 |
Correct |
377 ms |
94036 KB |
Output is correct |
91 |
Correct |
380 ms |
94192 KB |
Output is correct |
92 |
Correct |
364 ms |
93960 KB |
Output is correct |