#include "books.h"
#include<bits/stdc++.h>
#define fr first
#define sc second
#define all(s) s.begin(), s.end()
using namespace std;
const int nmax = 1000005;
long long n, ans = 0, viz[nmax], comp = 0, cost, diff = 1e18, el = 0;
vector<pair<long long,long long>>ranges;
pair<long long,long long>range[nmax];
vector<long long>nod[nmax];
long long minimum_walk(vector<int> p, int s) {
n = (int)p.size();
for(int i=0;i<n;i++){
ans += abs(p[i] - i);
}
for(int i=0;i<n;i++){
if(!viz[i]){
int maxi = i, mini = i;
comp++;
viz[i] = comp;
int curr = p[i];
while(curr != i){
viz[curr] = comp;
maxi = max(maxi, curr);
mini = min(mini, curr);
curr = p[curr];
}
range[comp].fr = mini;
range[comp].sc = maxi;
ranges.push_back({mini, maxi});
}
}
for(int i=range[viz[s]].fr;i<=range[viz[s]].sc;i++){
if(p[i] != i && abs(s - i) < diff){
diff = abs(s - i);
el = i;
}
}
if(diff == 1e18){
diff = 0;
}
else{
diff *= 2LL;
s = el;
}
while(ranges.size() && ranges.back().fr == ranges.back().sc && ranges.back().sc > s){
ranges.pop_back();
}
reverse(all(ranges));
while(ranges.size() && ranges.back().fr == ranges.back().sc && ranges.back().sc < s){
ranges.pop_back();
}
sort(all(ranges));
deque<pair<long long,long long>>Q;
for(auto it : ranges){
Q.push_back(it);
}
queue<pair<long long, long long>>rn;
while(Q.size()){
cost += 2;
rn.push(Q.front());
Q.pop_front();
while(rn.size()){
auto it = rn.front();
rn.pop();
while(Q.size() && Q.front().fr <= it.sc){
rn.push(Q.front());
Q.pop_front();
}
}
}
cost -= 2LL;
return ans + cost + diff;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23756 KB |
Output is correct |
2 |
Correct |
16 ms |
23756 KB |
Output is correct |
3 |
Correct |
16 ms |
23792 KB |
Output is correct |
4 |
Correct |
16 ms |
23728 KB |
Output is correct |
5 |
Correct |
16 ms |
23756 KB |
Output is correct |
6 |
Correct |
17 ms |
23792 KB |
Output is correct |
7 |
Correct |
15 ms |
23756 KB |
Output is correct |
8 |
Correct |
16 ms |
23776 KB |
Output is correct |
9 |
Correct |
17 ms |
23756 KB |
Output is correct |
10 |
Correct |
16 ms |
23772 KB |
Output is correct |
11 |
Correct |
16 ms |
23756 KB |
Output is correct |
12 |
Correct |
16 ms |
23788 KB |
Output is correct |
13 |
Correct |
17 ms |
23792 KB |
Output is correct |
14 |
Correct |
17 ms |
23756 KB |
Output is correct |
15 |
Correct |
16 ms |
23744 KB |
Output is correct |
16 |
Correct |
16 ms |
23736 KB |
Output is correct |
17 |
Correct |
16 ms |
23688 KB |
Output is correct |
18 |
Correct |
16 ms |
23704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23756 KB |
Output is correct |
2 |
Correct |
16 ms |
23756 KB |
Output is correct |
3 |
Correct |
16 ms |
23792 KB |
Output is correct |
4 |
Correct |
16 ms |
23728 KB |
Output is correct |
5 |
Correct |
16 ms |
23756 KB |
Output is correct |
6 |
Correct |
17 ms |
23792 KB |
Output is correct |
7 |
Correct |
15 ms |
23756 KB |
Output is correct |
8 |
Correct |
16 ms |
23776 KB |
Output is correct |
9 |
Correct |
17 ms |
23756 KB |
Output is correct |
10 |
Correct |
16 ms |
23772 KB |
Output is correct |
11 |
Correct |
16 ms |
23756 KB |
Output is correct |
12 |
Correct |
16 ms |
23788 KB |
Output is correct |
13 |
Correct |
17 ms |
23792 KB |
Output is correct |
14 |
Correct |
17 ms |
23756 KB |
Output is correct |
15 |
Correct |
16 ms |
23744 KB |
Output is correct |
16 |
Correct |
16 ms |
23736 KB |
Output is correct |
17 |
Correct |
16 ms |
23688 KB |
Output is correct |
18 |
Correct |
16 ms |
23704 KB |
Output is correct |
19 |
Correct |
16 ms |
23760 KB |
Output is correct |
20 |
Correct |
16 ms |
23804 KB |
Output is correct |
21 |
Correct |
16 ms |
23760 KB |
Output is correct |
22 |
Correct |
16 ms |
23840 KB |
Output is correct |
23 |
Correct |
16 ms |
23796 KB |
Output is correct |
24 |
Correct |
16 ms |
23720 KB |
Output is correct |
25 |
Correct |
16 ms |
23828 KB |
Output is correct |
26 |
Correct |
17 ms |
23812 KB |
Output is correct |
27 |
Correct |
17 ms |
23740 KB |
Output is correct |
28 |
Correct |
16 ms |
23812 KB |
Output is correct |
29 |
Correct |
16 ms |
23724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23756 KB |
Output is correct |
2 |
Correct |
16 ms |
23756 KB |
Output is correct |
3 |
Correct |
16 ms |
23792 KB |
Output is correct |
4 |
Correct |
16 ms |
23728 KB |
Output is correct |
5 |
Correct |
16 ms |
23756 KB |
Output is correct |
6 |
Correct |
17 ms |
23792 KB |
Output is correct |
7 |
Correct |
15 ms |
23756 KB |
Output is correct |
8 |
Correct |
16 ms |
23776 KB |
Output is correct |
9 |
Correct |
17 ms |
23756 KB |
Output is correct |
10 |
Correct |
16 ms |
23772 KB |
Output is correct |
11 |
Correct |
16 ms |
23756 KB |
Output is correct |
12 |
Correct |
16 ms |
23788 KB |
Output is correct |
13 |
Correct |
17 ms |
23792 KB |
Output is correct |
14 |
Correct |
17 ms |
23756 KB |
Output is correct |
15 |
Correct |
16 ms |
23744 KB |
Output is correct |
16 |
Correct |
16 ms |
23736 KB |
Output is correct |
17 |
Correct |
16 ms |
23688 KB |
Output is correct |
18 |
Correct |
16 ms |
23704 KB |
Output is correct |
19 |
Correct |
16 ms |
23760 KB |
Output is correct |
20 |
Correct |
16 ms |
23804 KB |
Output is correct |
21 |
Correct |
16 ms |
23760 KB |
Output is correct |
22 |
Correct |
16 ms |
23840 KB |
Output is correct |
23 |
Correct |
16 ms |
23796 KB |
Output is correct |
24 |
Correct |
16 ms |
23720 KB |
Output is correct |
25 |
Correct |
16 ms |
23828 KB |
Output is correct |
26 |
Correct |
17 ms |
23812 KB |
Output is correct |
27 |
Correct |
17 ms |
23740 KB |
Output is correct |
28 |
Correct |
16 ms |
23812 KB |
Output is correct |
29 |
Correct |
16 ms |
23724 KB |
Output is correct |
30 |
Correct |
207 ms |
39364 KB |
Output is correct |
31 |
Correct |
258 ms |
39340 KB |
Output is correct |
32 |
Correct |
173 ms |
70844 KB |
Output is correct |
33 |
Correct |
207 ms |
72532 KB |
Output is correct |
34 |
Correct |
215 ms |
72388 KB |
Output is correct |
35 |
Correct |
188 ms |
65556 KB |
Output is correct |
36 |
Correct |
163 ms |
51632 KB |
Output is correct |
37 |
Correct |
144 ms |
40992 KB |
Output is correct |
38 |
Correct |
141 ms |
39724 KB |
Output is correct |
39 |
Correct |
145 ms |
39712 KB |
Output is correct |
40 |
Correct |
150 ms |
39352 KB |
Output is correct |
41 |
Correct |
187 ms |
39388 KB |
Output is correct |
42 |
Correct |
157 ms |
39416 KB |
Output is correct |
43 |
Correct |
210 ms |
75272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
23756 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23756 KB |
Output is correct |
2 |
Correct |
16 ms |
23756 KB |
Output is correct |
3 |
Correct |
16 ms |
23792 KB |
Output is correct |
4 |
Correct |
16 ms |
23728 KB |
Output is correct |
5 |
Correct |
16 ms |
23756 KB |
Output is correct |
6 |
Correct |
17 ms |
23792 KB |
Output is correct |
7 |
Correct |
15 ms |
23756 KB |
Output is correct |
8 |
Correct |
16 ms |
23776 KB |
Output is correct |
9 |
Correct |
17 ms |
23756 KB |
Output is correct |
10 |
Correct |
16 ms |
23772 KB |
Output is correct |
11 |
Correct |
16 ms |
23756 KB |
Output is correct |
12 |
Correct |
16 ms |
23788 KB |
Output is correct |
13 |
Correct |
17 ms |
23792 KB |
Output is correct |
14 |
Correct |
17 ms |
23756 KB |
Output is correct |
15 |
Correct |
16 ms |
23744 KB |
Output is correct |
16 |
Correct |
16 ms |
23736 KB |
Output is correct |
17 |
Correct |
16 ms |
23688 KB |
Output is correct |
18 |
Correct |
16 ms |
23704 KB |
Output is correct |
19 |
Correct |
16 ms |
23760 KB |
Output is correct |
20 |
Correct |
16 ms |
23804 KB |
Output is correct |
21 |
Correct |
16 ms |
23760 KB |
Output is correct |
22 |
Correct |
16 ms |
23840 KB |
Output is correct |
23 |
Correct |
16 ms |
23796 KB |
Output is correct |
24 |
Correct |
16 ms |
23720 KB |
Output is correct |
25 |
Correct |
16 ms |
23828 KB |
Output is correct |
26 |
Correct |
17 ms |
23812 KB |
Output is correct |
27 |
Correct |
17 ms |
23740 KB |
Output is correct |
28 |
Correct |
16 ms |
23812 KB |
Output is correct |
29 |
Correct |
16 ms |
23724 KB |
Output is correct |
30 |
Correct |
207 ms |
39364 KB |
Output is correct |
31 |
Correct |
258 ms |
39340 KB |
Output is correct |
32 |
Correct |
173 ms |
70844 KB |
Output is correct |
33 |
Correct |
207 ms |
72532 KB |
Output is correct |
34 |
Correct |
215 ms |
72388 KB |
Output is correct |
35 |
Correct |
188 ms |
65556 KB |
Output is correct |
36 |
Correct |
163 ms |
51632 KB |
Output is correct |
37 |
Correct |
144 ms |
40992 KB |
Output is correct |
38 |
Correct |
141 ms |
39724 KB |
Output is correct |
39 |
Correct |
145 ms |
39712 KB |
Output is correct |
40 |
Correct |
150 ms |
39352 KB |
Output is correct |
41 |
Correct |
187 ms |
39388 KB |
Output is correct |
42 |
Correct |
157 ms |
39416 KB |
Output is correct |
43 |
Correct |
210 ms |
75272 KB |
Output is correct |
44 |
Incorrect |
16 ms |
23756 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |