#include "books.h"
#include <bits/stdc++.h>
using namespace std;
const int N=1e6, LG=20;
int tl, tr;
int vis[N], n, stmin[LG][N], stmax[LG][N];
pair<int, int> ll[N], rr[N];
int check[N];
long long ans=0;
int getmin(int l, int r){
int lg=__lg(r-l+1);
return min(stmin[lg][l], stmin[lg][r-(1<<lg)+1]);
}
int getmax(int l, int r){
int lg=__lg(r-l+1);
return max(stmax[lg][l], stmax[lg][r-(1<<lg)+1]);
}
void extend(int &l, int &r){
while (1){
int nl=getmin(l, r), nr=getmax(l, r);
if (nl==l && nr==r) break;
l=nl; r=nr;
}
}
void dp(int l, int r){
extend(l, r);
if (l==tl && r==tr) return;
int l1=tl, r1=r, l2=l, r2=tr;
if (ll[l].first || ll[l].second){
l1=ll[l].first;
}
if (rr[r].first || rr[r].second){
r2=rr[r].second;
}
int c1=check[l]-check[l1];
int c2=check[r2]-check[r];
if (l1==l) c1=1e9;
if (r2==r) c2=1e9;
ans+=min(c1, c2)*2;
if (c1<=c2) return dp(l1, r1);
return dp(l2, r2);
}
long long minimum_walk(vector<int> p, int s) {
n=p.size();
for (int i=0; i<n; ++i) if (!vis[i]){
vector<int> cc;
int u=i;
stmin[0][i]=i; stmax[0][i]=i;
while (!vis[u]){
vis[u]=1;
cc.push_back(u);
stmin[0][i]=min(stmin[0][i], u);
stmax[0][i]=max(stmax[0][i], u);
ans+=abs(u-p[u]);
u=p[u];
}
if (stmin[0][i]<s && s<stmax[0][i]) ll[stmin[0][i]]=rr[stmax[0][i]]={stmin[0][i], stmax[0][i]};
else{
int l=stmin[0][i], r=stmax[0][i];
++check[l]; --check[r];
}
for (int j:cc) stmin[0][j]=stmin[0][i], stmax[0][j]=stmax[0][i];
}
partial_sum(check, check+n, check);
for (int i=n-1; i>0; --i) check[i]=!check[i-1];
check[0]=0;
partial_sum(check, check+n, check);
for (int i=1; i<=s; ++i){
if (ll[i].first==0 && ll[i].second==0) ll[i]=ll[i-1];
}
for (int i=n-2; i>=s; --i){
if (rr[i].first==0 && rr[i].second==0) rr[i]=rr[i+1];
}
for (int i=1; i<LG; ++i) for (int j=0; j+(1<<i)-1<n; ++j){
stmin[i][j]=min(stmin[i-1][j], stmin[i-1][j+(1<<(i-1))]);
stmax[i][j]=max(stmax[i-1][j], stmax[i-1][j+(1<<(i-1))]);
}
tl=0, tr=n-1;
while (tl<s && stmin[0][tl]==tl && stmax[0][tl]==tl) ++tl;
while (tr>s && stmin[0][tr]==tr && stmax[0][tr]==tr) --tr;
dp(s, s);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
1 ms |
14684 KB |
Output is correct |
3 |
Correct |
1 ms |
14684 KB |
Output is correct |
4 |
Correct |
1 ms |
10684 KB |
Output is correct |
5 |
Correct |
1 ms |
14680 KB |
Output is correct |
6 |
Correct |
1 ms |
14684 KB |
Output is correct |
7 |
Correct |
1 ms |
14680 KB |
Output is correct |
8 |
Correct |
1 ms |
14684 KB |
Output is correct |
9 |
Correct |
1 ms |
14680 KB |
Output is correct |
10 |
Correct |
2 ms |
14680 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
14684 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
14684 KB |
Output is correct |
16 |
Correct |
2 ms |
14684 KB |
Output is correct |
17 |
Correct |
2 ms |
14684 KB |
Output is correct |
18 |
Correct |
1 ms |
14684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
1 ms |
14684 KB |
Output is correct |
3 |
Correct |
1 ms |
14684 KB |
Output is correct |
4 |
Correct |
1 ms |
10684 KB |
Output is correct |
5 |
Correct |
1 ms |
14680 KB |
Output is correct |
6 |
Correct |
1 ms |
14684 KB |
Output is correct |
7 |
Correct |
1 ms |
14680 KB |
Output is correct |
8 |
Correct |
1 ms |
14684 KB |
Output is correct |
9 |
Correct |
1 ms |
14680 KB |
Output is correct |
10 |
Correct |
2 ms |
14680 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
14684 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
14684 KB |
Output is correct |
16 |
Correct |
2 ms |
14684 KB |
Output is correct |
17 |
Correct |
2 ms |
14684 KB |
Output is correct |
18 |
Correct |
1 ms |
14684 KB |
Output is correct |
19 |
Correct |
4 ms |
43356 KB |
Output is correct |
20 |
Correct |
4 ms |
43356 KB |
Output is correct |
21 |
Correct |
4 ms |
43508 KB |
Output is correct |
22 |
Correct |
4 ms |
43356 KB |
Output is correct |
23 |
Correct |
4 ms |
43508 KB |
Output is correct |
24 |
Correct |
4 ms |
43352 KB |
Output is correct |
25 |
Correct |
4 ms |
43356 KB |
Output is correct |
26 |
Correct |
4 ms |
43356 KB |
Output is correct |
27 |
Correct |
4 ms |
43464 KB |
Output is correct |
28 |
Correct |
4 ms |
43356 KB |
Output is correct |
29 |
Correct |
4 ms |
43356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
1 ms |
14684 KB |
Output is correct |
3 |
Correct |
1 ms |
14684 KB |
Output is correct |
4 |
Correct |
1 ms |
10684 KB |
Output is correct |
5 |
Correct |
1 ms |
14680 KB |
Output is correct |
6 |
Correct |
1 ms |
14684 KB |
Output is correct |
7 |
Correct |
1 ms |
14680 KB |
Output is correct |
8 |
Correct |
1 ms |
14684 KB |
Output is correct |
9 |
Correct |
1 ms |
14680 KB |
Output is correct |
10 |
Correct |
2 ms |
14680 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
14684 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
14684 KB |
Output is correct |
16 |
Correct |
2 ms |
14684 KB |
Output is correct |
17 |
Correct |
2 ms |
14684 KB |
Output is correct |
18 |
Correct |
1 ms |
14684 KB |
Output is correct |
19 |
Correct |
4 ms |
43356 KB |
Output is correct |
20 |
Correct |
4 ms |
43356 KB |
Output is correct |
21 |
Correct |
4 ms |
43508 KB |
Output is correct |
22 |
Correct |
4 ms |
43356 KB |
Output is correct |
23 |
Correct |
4 ms |
43508 KB |
Output is correct |
24 |
Correct |
4 ms |
43352 KB |
Output is correct |
25 |
Correct |
4 ms |
43356 KB |
Output is correct |
26 |
Correct |
4 ms |
43356 KB |
Output is correct |
27 |
Correct |
4 ms |
43464 KB |
Output is correct |
28 |
Correct |
4 ms |
43356 KB |
Output is correct |
29 |
Correct |
4 ms |
43356 KB |
Output is correct |
30 |
Correct |
140 ms |
182984 KB |
Output is correct |
31 |
Correct |
132 ms |
183252 KB |
Output is correct |
32 |
Correct |
119 ms |
180488 KB |
Output is correct |
33 |
Correct |
131 ms |
180564 KB |
Output is correct |
34 |
Correct |
123 ms |
181260 KB |
Output is correct |
35 |
Correct |
125 ms |
180560 KB |
Output is correct |
36 |
Correct |
118 ms |
180564 KB |
Output is correct |
37 |
Correct |
108 ms |
180560 KB |
Output is correct |
38 |
Correct |
110 ms |
180560 KB |
Output is correct |
39 |
Correct |
110 ms |
181328 KB |
Output is correct |
40 |
Correct |
111 ms |
181532 KB |
Output is correct |
41 |
Correct |
125 ms |
181352 KB |
Output is correct |
42 |
Correct |
123 ms |
181388 KB |
Output is correct |
43 |
Correct |
130 ms |
180820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
47452 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3510' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
1 ms |
14684 KB |
Output is correct |
3 |
Correct |
1 ms |
14684 KB |
Output is correct |
4 |
Correct |
1 ms |
10684 KB |
Output is correct |
5 |
Correct |
1 ms |
14680 KB |
Output is correct |
6 |
Correct |
1 ms |
14684 KB |
Output is correct |
7 |
Correct |
1 ms |
14680 KB |
Output is correct |
8 |
Correct |
1 ms |
14684 KB |
Output is correct |
9 |
Correct |
1 ms |
14680 KB |
Output is correct |
10 |
Correct |
2 ms |
14680 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
14684 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
14684 KB |
Output is correct |
16 |
Correct |
2 ms |
14684 KB |
Output is correct |
17 |
Correct |
2 ms |
14684 KB |
Output is correct |
18 |
Correct |
1 ms |
14684 KB |
Output is correct |
19 |
Correct |
4 ms |
43356 KB |
Output is correct |
20 |
Correct |
4 ms |
43356 KB |
Output is correct |
21 |
Correct |
4 ms |
43508 KB |
Output is correct |
22 |
Correct |
4 ms |
43356 KB |
Output is correct |
23 |
Correct |
4 ms |
43508 KB |
Output is correct |
24 |
Correct |
4 ms |
43352 KB |
Output is correct |
25 |
Correct |
4 ms |
43356 KB |
Output is correct |
26 |
Correct |
4 ms |
43356 KB |
Output is correct |
27 |
Correct |
4 ms |
43464 KB |
Output is correct |
28 |
Correct |
4 ms |
43356 KB |
Output is correct |
29 |
Correct |
4 ms |
43356 KB |
Output is correct |
30 |
Correct |
140 ms |
182984 KB |
Output is correct |
31 |
Correct |
132 ms |
183252 KB |
Output is correct |
32 |
Correct |
119 ms |
180488 KB |
Output is correct |
33 |
Correct |
131 ms |
180564 KB |
Output is correct |
34 |
Correct |
123 ms |
181260 KB |
Output is correct |
35 |
Correct |
125 ms |
180560 KB |
Output is correct |
36 |
Correct |
118 ms |
180564 KB |
Output is correct |
37 |
Correct |
108 ms |
180560 KB |
Output is correct |
38 |
Correct |
110 ms |
180560 KB |
Output is correct |
39 |
Correct |
110 ms |
181328 KB |
Output is correct |
40 |
Correct |
111 ms |
181532 KB |
Output is correct |
41 |
Correct |
125 ms |
181352 KB |
Output is correct |
42 |
Correct |
123 ms |
181388 KB |
Output is correct |
43 |
Correct |
130 ms |
180820 KB |
Output is correct |
44 |
Incorrect |
4 ms |
47452 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3510' |
45 |
Halted |
0 ms |
0 KB |
- |