//#include "books.h"
#include<bits/stdc++.h>
using namespace std;
const int INF=1e9+7;
long long minimum_walk(vector<int> p, int s) {
int n=p.size();
long long ans=0;
vector<bool> vis(n, false);
vector<bool> ends(n);
vector<bool> starts(n);
vector<int> cycle(n);
set<pair<int,int> > ranges;
for(int i=0; i<n; i++){
if(vis[i]) continue;
int v=i;
int l=i;
int r=i;
int cnt=0;
while(!vis[v]){
vis[v]=true;
cycle[v] = i;
l=min(l, v);
r=max(r, v);
cnt++;
ans += abs(p[v]-v);
v=p[v];
}
ends[r] = true;
starts[l] = true;
if(cnt!=1 || i==s){
ranges.insert({l, r});
}
}
//start:
set<int> free;
set<int> active;
for(auto [l, r]: ranges){
auto it=active.upper_bound(r);
if(it==active.end()){
free.insert(l);
free.insert(r);
active.insert(r);
}
}
free.insert(s);
int l=s; int r=s;
vector<int> queue; queue.push_back(s);
vis.assign(n, false);
while(!free.empty()){
if(!queue.empty()){
int cur=queue.back();
queue.pop_back();
while(!vis[cur]){
vis[cur]=true;
free.erase(cur);
l=min(l, cur);
r=max(r, cur);
cur = p[cur];
}
vector<int> neu;
auto it=free.upper_bound(l);
while(it!=free.end() && *it<r){
neu.push_back(*it);
it=next(it);
}
for(auto e: neu){
queue.push_back(e);
free.erase(e);
}
}
else{
auto itleft = free.upper_bound(l);
int left=-1; int distleft=INF;
if(itleft!=free.begin()){
left = *prev(itleft);
distleft = l-left;
}
auto itright = free.upper_bound(r);
int right=-1; int distright = INF;
if(itright!=free.end()){
right = *itright;
distright = right -r;
}
if((distleft <= distright || right==-1) && left!=-1){
ans += 2*distleft;
free.erase(left);
queue.push_back(left);
}
else if(right!=-1){
ans += 2*distright;
free.erase(right);
queue.push_back(right);
}
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
122 ms |
12400 KB |
Output is correct |
31 |
Correct |
111 ms |
12408 KB |
Output is correct |
32 |
Correct |
87 ms |
12284 KB |
Output is correct |
33 |
Correct |
281 ms |
47564 KB |
Output is correct |
34 |
Correct |
279 ms |
47572 KB |
Output is correct |
35 |
Correct |
274 ms |
50252 KB |
Output is correct |
36 |
Correct |
198 ms |
35536 KB |
Output is correct |
37 |
Correct |
97 ms |
15052 KB |
Output is correct |
38 |
Correct |
87 ms |
12876 KB |
Output is correct |
39 |
Correct |
90 ms |
12812 KB |
Output is correct |
40 |
Correct |
89 ms |
12408 KB |
Output is correct |
41 |
Correct |
103 ms |
12368 KB |
Output is correct |
42 |
Correct |
104 ms |
12396 KB |
Output is correct |
43 |
Correct |
129 ms |
24012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 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 |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
122 ms |
12400 KB |
Output is correct |
31 |
Correct |
111 ms |
12408 KB |
Output is correct |
32 |
Correct |
87 ms |
12284 KB |
Output is correct |
33 |
Correct |
281 ms |
47564 KB |
Output is correct |
34 |
Correct |
279 ms |
47572 KB |
Output is correct |
35 |
Correct |
274 ms |
50252 KB |
Output is correct |
36 |
Correct |
198 ms |
35536 KB |
Output is correct |
37 |
Correct |
97 ms |
15052 KB |
Output is correct |
38 |
Correct |
87 ms |
12876 KB |
Output is correct |
39 |
Correct |
90 ms |
12812 KB |
Output is correct |
40 |
Correct |
89 ms |
12408 KB |
Output is correct |
41 |
Correct |
103 ms |
12368 KB |
Output is correct |
42 |
Correct |
104 ms |
12396 KB |
Output is correct |
43 |
Correct |
129 ms |
24012 KB |
Output is correct |
44 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3594' |
45 |
Halted |
0 ms |
0 KB |
- |