#include "books.h"
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
typedef long long int ll;
typedef pair<int,int> pii;
const int MAXN = 1e6+1;
int par[MAXN];
int find(int x){
if(x==par[x])return x;
return par[x] = find(par[x]);
}
ll minimum_walk(vector<int> p, int s) {
int n = sz(p);
ll ans = 0;
for(int i=0;i<n;i++){
ans+=abs(i-p[i]);
par[i] = i;
}
for(int i=0;i<n;i++){
if(i==p[i])continue;
int mn = min(i,p[i]);
int mx = max(i,p[i]);
int j = mn;
while(find(j)+1 <= mx){
j = find(j);
par[j] = j+1;
}
}
vector<pii>lf,rg;
pii md = {-1,-1};
for(int i=0;i<n;i++){
if(i==p[i])continue;
int j = i;
while(j+1<n && find(i) == find(j+1))j++;
if(j<=s)lf.pb({i,j});
else if(i>=s)rg.pb({i,j});
else md = {i,j};
i = j;
}
ll add = 0;
if(md.fi != -1){
add+=min(s-md.fi,md.se-s);
lf.pb({md.fi,md.fi});
rg.insert(rg.begin(),{md.se,md.se});
}else{
lf.pb({s,s});
rg.insert(rg.begin(),{s,s});
}
//for(pii x:lf)cout<<x.fi<<" "<<x.se<<'\n';
//cout<<add<<'\n';
for(int i=0;i+1<sz(lf);i++)add+=lf[i+1].fi - lf[i].se;
//cout<<add<<'\n';
for(int i=0;i+1<sz(rg);i++)add+=rg[i+1].fi - rg[i].se;
//cout<<add<<'\n';
ans+=2*add;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
93 ms |
20348 KB |
Output is correct |
31 |
Correct |
98 ms |
19020 KB |
Output is correct |
32 |
Correct |
74 ms |
11908 KB |
Output is correct |
33 |
Correct |
87 ms |
14152 KB |
Output is correct |
34 |
Correct |
88 ms |
14192 KB |
Output is correct |
35 |
Correct |
92 ms |
14184 KB |
Output is correct |
36 |
Correct |
121 ms |
13124 KB |
Output is correct |
37 |
Correct |
88 ms |
12168 KB |
Output is correct |
38 |
Correct |
93 ms |
12024 KB |
Output is correct |
39 |
Correct |
87 ms |
12076 KB |
Output is correct |
40 |
Correct |
100 ms |
12404 KB |
Output is correct |
41 |
Correct |
113 ms |
18300 KB |
Output is correct |
42 |
Correct |
94 ms |
15268 KB |
Output is correct |
43 |
Correct |
91 ms |
20344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3594' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
93 ms |
20348 KB |
Output is correct |
31 |
Correct |
98 ms |
19020 KB |
Output is correct |
32 |
Correct |
74 ms |
11908 KB |
Output is correct |
33 |
Correct |
87 ms |
14152 KB |
Output is correct |
34 |
Correct |
88 ms |
14192 KB |
Output is correct |
35 |
Correct |
92 ms |
14184 KB |
Output is correct |
36 |
Correct |
121 ms |
13124 KB |
Output is correct |
37 |
Correct |
88 ms |
12168 KB |
Output is correct |
38 |
Correct |
93 ms |
12024 KB |
Output is correct |
39 |
Correct |
87 ms |
12076 KB |
Output is correct |
40 |
Correct |
100 ms |
12404 KB |
Output is correct |
41 |
Correct |
113 ms |
18300 KB |
Output is correct |
42 |
Correct |
94 ms |
15268 KB |
Output is correct |
43 |
Correct |
91 ms |
20344 KB |
Output is correct |
44 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3594' |
45 |
Halted |
0 ms |
0 KB |
- |