#include <bits/stdc++.h>
using namespace std;
const int c=1000002;
bool v[c];
long long sum;
int n, t[c], kezd, veg, kis, nagy, bh=c, jh, mini[c], maxi[c], x[3], y[3];
vector<int> akt;
void dfs(int a) {
if (v[a]) return;
v[a]=true, akt.push_back(a);
kis=min(kis, a), nagy=max(nagy, a);
dfs(t[a]);
}
void le() {
int pos=mini[kezd], d=kezd, db=0, v=maxi[kezd];
while(bh<pos && v<=veg) {
while(d>pos) {
d--;
pos=min(pos, mini[d]);
v=max(v, maxi[d]);
}
if (bh<pos && v<=veg) {
db++;
pos--, pos=mini[pos], v=max(v, maxi[pos]);
}
while(d>pos) {
d--;
pos=min(pos, mini[d]);
v=max(v, maxi[d]);
}
}
x[0]=db;
if (v>veg) x[1]=pos, x[2]=v;
else x[1]=-1;
}
void fel() {
int pos=maxi[veg], d=veg, db=0, k=mini[kezd];
while(kezd<=k && pos<jh) {
while(d<pos) {
d++;
pos=max(pos, maxi[d]);
k=min(k, mini[d]);
}
if (k<=kezd && pos<jh) {
db++;
pos++, pos=maxi[pos], k=min(k, mini[pos]);
}
while(d<pos) {
d++;
pos=max(pos, maxi[d]);
k=min(k, mini[d]);
}
}
y[0]=db;
}
long long minimum_walk(vector<int> p, int s) {
n=p.size(), kezd=s, veg=s, kis=s, nagy=s, bh, jh;
for (int i=0; i<n; i++) {
t[i]=p[i], sum+=abs(p[i]-i);
}
for (int i=0; i<n; i++) {
if (t[i]!=i) {
if (bh==c) bh=i;
jh=i;
}
kis=n, nagy=0;
dfs(i);
for (int i=0; i<akt.size(); i++) mini[akt[i]]=kis, maxi[akt[i]]=nagy;
akt.clear();
}
while(bh<kezd || veg<jh) {
int p=mini[kezd], q=maxi[veg];
while(p<kezd || veg<q) {
p=min({p, mini[kezd], mini[veg]}), q=max({q, maxi[kezd], maxi[veg]});
if (kezd>p) kezd--;
if (veg<q) veg++;
}
le(), fel();
if (x[1]==-1) {
sum+=2*(x[0]+y[0]);
break;
}
sum+=2*min(x[0], y[0]);
while(x[1]<kezd || veg<x[2]) {
x[1]=min({x[1], mini[kezd], mini[veg]}), x[2]=max({x[2], maxi[kezd], maxi[veg]});
if (kezd>x[1]) kezd--;
if (veg<x[2]) veg++;
}
}
return sum;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:58:51: warning: right operand of comma operator has no effect [-Wunused-value]
58 | n=p.size(), kezd=s, veg=s, kis=s, nagy=s, bh, jh;
| ^~
books.cpp:58:53: warning: right operand of comma operator has no effect [-Wunused-value]
58 | n=p.size(), kezd=s, veg=s, kis=s, nagy=s, bh, jh;
| ^
books.cpp:69:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int i=0; i<akt.size(); i++) mini[akt[i]]=kis, maxi[akt[i]]=nagy;
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 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 |
0 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 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 |
0 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
276 ms |
48432 KB |
Output is correct |
31 |
Correct |
266 ms |
34536 KB |
Output is correct |
32 |
Correct |
168 ms |
20812 KB |
Output is correct |
33 |
Correct |
180 ms |
20856 KB |
Output is correct |
34 |
Correct |
178 ms |
20856 KB |
Output is correct |
35 |
Correct |
181 ms |
20932 KB |
Output is correct |
36 |
Correct |
180 ms |
20856 KB |
Output is correct |
37 |
Correct |
181 ms |
20984 KB |
Output is correct |
38 |
Correct |
181 ms |
21112 KB |
Output is correct |
39 |
Correct |
182 ms |
21112 KB |
Output is correct |
40 |
Correct |
192 ms |
22388 KB |
Output is correct |
41 |
Correct |
236 ms |
32744 KB |
Output is correct |
42 |
Correct |
218 ms |
26604 KB |
Output is correct |
43 |
Correct |
182 ms |
20856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
416 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 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 |
0 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
276 ms |
48432 KB |
Output is correct |
31 |
Correct |
266 ms |
34536 KB |
Output is correct |
32 |
Correct |
168 ms |
20812 KB |
Output is correct |
33 |
Correct |
180 ms |
20856 KB |
Output is correct |
34 |
Correct |
178 ms |
20856 KB |
Output is correct |
35 |
Correct |
181 ms |
20932 KB |
Output is correct |
36 |
Correct |
180 ms |
20856 KB |
Output is correct |
37 |
Correct |
181 ms |
20984 KB |
Output is correct |
38 |
Correct |
181 ms |
21112 KB |
Output is correct |
39 |
Correct |
182 ms |
21112 KB |
Output is correct |
40 |
Correct |
192 ms |
22388 KB |
Output is correct |
41 |
Correct |
236 ms |
32744 KB |
Output is correct |
42 |
Correct |
218 ms |
26604 KB |
Output is correct |
43 |
Correct |
182 ms |
20856 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 |
416 KB |
Output is correct |
63 |
Correct |
1 ms |
384 KB |
Output is correct |
64 |
Correct |
175 ms |
20856 KB |
Output is correct |
65 |
Correct |
174 ms |
20856 KB |
Output is correct |
66 |
Correct |
181 ms |
20940 KB |
Output is correct |
67 |
Correct |
184 ms |
20984 KB |
Output is correct |
68 |
Correct |
18 ms |
2432 KB |
Output is correct |
69 |
Correct |
18 ms |
2432 KB |
Output is correct |
70 |
Correct |
18 ms |
2432 KB |
Output is correct |
71 |
Correct |
18 ms |
2424 KB |
Output is correct |
72 |
Correct |
17 ms |
2432 KB |
Output is correct |
73 |
Correct |
19 ms |
2816 KB |
Output is correct |
74 |
Correct |
180 ms |
20856 KB |
Output is correct |
75 |
Correct |
185 ms |
20856 KB |
Output is correct |
76 |
Correct |
180 ms |
20984 KB |
Output is correct |
77 |
Correct |
180 ms |
20984 KB |
Output is correct |
78 |
Correct |
178 ms |
20856 KB |
Output is correct |
79 |
Correct |
182 ms |
20856 KB |
Output is correct |
80 |
Correct |
171 ms |
20856 KB |
Output is correct |
81 |
Correct |
175 ms |
20856 KB |
Output is correct |
82 |
Correct |
176 ms |
20856 KB |
Output is correct |
83 |
Correct |
195 ms |
20856 KB |
Output is correct |
84 |
Correct |
182 ms |
20856 KB |
Output is correct |
85 |
Correct |
178 ms |
20856 KB |
Output is correct |
86 |
Correct |
185 ms |
20856 KB |
Output is correct |
87 |
Correct |
188 ms |
20984 KB |
Output is correct |
88 |
Correct |
194 ms |
20804 KB |
Output is correct |
89 |
Correct |
187 ms |
20856 KB |
Output is correct |
90 |
Correct |
197 ms |
20984 KB |
Output is correct |
91 |
Correct |
188 ms |
20856 KB |
Output is correct |
92 |
Correct |
191 ms |
21240 KB |
Output is correct |