#include<bits/stdc++.h>
#include "books.h"
using namespace std;
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T>
using matrix = vector<vector<T>>;
const int INF = 1e9;
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size();
ll resp =0;
for(int i = 0; i < n; i++){
resp+=abs(i-p[i]);
}
vector<pii> crange(n);
vector<int> cycle(n), check(n);
auto dfs = [&](int id, int cid, auto dfs)->int{
cycle[id] = cid;
check[id] = 1;
if(check[p[id]])
return id;
return max(id,dfs(p[id],cid,dfs));
};
vector<int> isgood(n);
vector<pii> cycles;
for(int i = 0; i < n; i++){
if(!check[i]){
int r = dfs(i,i,dfs);
crange[i] = {i,r};
isgood[i] = cycles.empty() || cycles.back().ss < r;
if(r-i && isgood[i]){
if(cycles.size() && cycles.back().ss > i){
int l = cycles.back().ff;
cycles.pop_back();
cycles.push_back({l,r});
} else {
cycles.push_back({i,r});
}
}
}
}
for(int i = 0; i < (int)cycles.size()-1; i++){
resp+= 2*(cycles[i+1].ff-cycles[i].ss);
}
if(cycles.empty())
return resp;
if(s < cycles[0].ff || s > cycles.back().ss){
resp+=max(cycles[0].ff-s, s-cycles.back().ss)*2;
return resp;
}
matrix<pii> g(n);
for(int i = 0; i < n-1; i++){
int cur = cycle[i];
g[cur].push_back({cycle[i+1],2});
g[cycle[i+1]].push_back({cur,2});
}
vector<int> st;
for(int i = 0; i < n; i++){
if(st.empty()){
st.push_back(i);
goto END;
}
g[st.back()].push_back({cycle[i],0});
if(cycle[i]!=i)
goto END;
while(!st.empty() && crange[st.back()].ss < crange[i].ss){
g[i].push_back({st.back(),0});
st.pop_back();
}
st.push_back(i);
END:
if(i == crange[st.back()].ss)
st.pop_back();
}
priority_queue<pii,vector<pii>,greater<pii>> pq;
vector<int> dist(n,INF);
pq.push({0,cycle[s]});
dist[cycle[s]] = 0;
while(!pq.empty()){
int cur = pq.top().ss, curp = pq.top().ff;
pq.pop();
if(curp> dist[cur])
continue;
for(auto [viz,vizp] : g[cur]){
if(dist[viz] > vizp+dist[cur]){
dist[viz]= vizp+dist[cur];
pq.push({dist[viz],viz});
}
}
}
int add = INF;
for(int i = 0; i < n; i++){
if(isgood[i])
add = min(add,dist[i]);
}
resp+=add;
return resp;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:95:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
95 | for(auto [viz,vizp] : g[cur]){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 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 |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
288 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 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 |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
288 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
263 ms |
104640 KB |
Output is correct |
31 |
Correct |
301 ms |
93868 KB |
Output is correct |
32 |
Correct |
98 ms |
27636 KB |
Output is correct |
33 |
Correct |
104 ms |
29788 KB |
Output is correct |
34 |
Correct |
107 ms |
29796 KB |
Output is correct |
35 |
Correct |
263 ms |
94376 KB |
Output is correct |
36 |
Correct |
247 ms |
92660 KB |
Output is correct |
37 |
Correct |
182 ms |
89932 KB |
Output is correct |
38 |
Correct |
179 ms |
86520 KB |
Output is correct |
39 |
Correct |
165 ms |
86124 KB |
Output is correct |
40 |
Correct |
197 ms |
83496 KB |
Output is correct |
41 |
Correct |
198 ms |
92264 KB |
Output is correct |
42 |
Correct |
190 ms |
87472 KB |
Output is correct |
43 |
Correct |
454 ms |
100588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 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 |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
288 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
263 ms |
104640 KB |
Output is correct |
31 |
Correct |
301 ms |
93868 KB |
Output is correct |
32 |
Correct |
98 ms |
27636 KB |
Output is correct |
33 |
Correct |
104 ms |
29788 KB |
Output is correct |
34 |
Correct |
107 ms |
29796 KB |
Output is correct |
35 |
Correct |
263 ms |
94376 KB |
Output is correct |
36 |
Correct |
247 ms |
92660 KB |
Output is correct |
37 |
Correct |
182 ms |
89932 KB |
Output is correct |
38 |
Correct |
179 ms |
86520 KB |
Output is correct |
39 |
Correct |
165 ms |
86124 KB |
Output is correct |
40 |
Correct |
197 ms |
83496 KB |
Output is correct |
41 |
Correct |
198 ms |
92264 KB |
Output is correct |
42 |
Correct |
190 ms |
87472 KB |
Output is correct |
43 |
Correct |
454 ms |
100588 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
300 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
338 ms |
105360 KB |
Output is correct |
65 |
Correct |
360 ms |
106456 KB |
Output is correct |
66 |
Correct |
188 ms |
96024 KB |
Output is correct |
67 |
Correct |
213 ms |
95520 KB |
Output is correct |
68 |
Correct |
33 ms |
10316 KB |
Output is correct |
69 |
Correct |
22 ms |
9896 KB |
Output is correct |
70 |
Correct |
30 ms |
10316 KB |
Output is correct |
71 |
Correct |
32 ms |
10792 KB |
Output is correct |
72 |
Correct |
28 ms |
10828 KB |
Output is correct |
73 |
Correct |
17 ms |
9548 KB |
Output is correct |
74 |
Correct |
267 ms |
100948 KB |
Output is correct |
75 |
Correct |
265 ms |
101036 KB |
Output is correct |
76 |
Correct |
432 ms |
108076 KB |
Output is correct |
77 |
Correct |
416 ms |
107832 KB |
Output is correct |
78 |
Correct |
425 ms |
108096 KB |
Output is correct |
79 |
Correct |
417 ms |
107104 KB |
Output is correct |
80 |
Correct |
114 ms |
34404 KB |
Output is correct |
81 |
Correct |
258 ms |
104476 KB |
Output is correct |
82 |
Correct |
271 ms |
104148 KB |
Output is correct |
83 |
Correct |
298 ms |
104648 KB |
Output is correct |
84 |
Correct |
301 ms |
101444 KB |
Output is correct |
85 |
Correct |
272 ms |
99044 KB |
Output is correct |
86 |
Correct |
206 ms |
97296 KB |
Output is correct |
87 |
Correct |
302 ms |
111396 KB |
Output is correct |
88 |
Correct |
311 ms |
108976 KB |
Output is correct |
89 |
Correct |
316 ms |
104544 KB |
Output is correct |
90 |
Correct |
207 ms |
97004 KB |
Output is correct |
91 |
Correct |
177 ms |
94320 KB |
Output is correct |
92 |
Correct |
175 ms |
89808 KB |
Output is correct |