#include "books.h"
#define f first
#define s second
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> tab;
void solve(int a, int b, int &na, int &nb){
//cout<<a<<" "<<b<<" "<<na<<" "<<nb<<'\n';
int ta = na;
int tb = nb;
for(int i = na; i <= a; i++){
if(tab[i].f!=-1){
ta = min(ta, tab[i].f);
tb = max(tb, tab[i].s);
//cout<<i<<" "<<tab[i].f<<" "<<tab[i].s<<'\n';
}
}
for(int i = b; i <= nb; i++){
if(tab[i].f!=-1){
ta = min(ta, tab[i].f);
tb = max(tb, tab[i].s);
//cout<<i<<" "<<tab[i].f<<" "<<tab[i].s<<'\n';
}
}
if(ta!=na||nb!=tb) solve(na-1, nb+1, ta, tb);
na = ta;
nb = tb;
}
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size();
vector<int> odw(n);
long long wyn = 0;
vector<pair<int, int>> inp;
vector<pair<int, int>> inp2;
tab.resize(n, {-1, -1});
int globMax=-1e9, globMin=1e9;
for(int i = 0; i < n; i++){
int pt = i;
if(!odw[pt]){
if(p[pt]!=pt){
int mini = 1e9;
int maxi = -1e9;
vector<int> stck;
while(!odw[pt]){
stck.push_back(pt);
mini = min(mini, pt);
maxi = max(maxi, pt);
odw[pt] = true;
wyn+=abs(p[pt]-pt);
pt = p[pt];
}
globMax = max(globMax, maxi);
globMin = min(globMin, mini);
for(auto it:stck) tab[it] = {mini, maxi};
inp2.push_back({mini, -i});
inp2.push_back({maxi, i});
inp.push_back({mini, maxi});
}else{
tab[i] = {-1, -1};
odw[pt] = true;
}
}
}
inp.push_back({s, s});
sort(inp.begin(), inp.end());
int dal = -1;
int cA=-1, cB=-1;
bool bylo = false;
for(int i = 0; i < inp.size(); i++){
while(dal>inp[i].f){
dal = max(dal, inp[i].s);
i++;
if(i>=inp.size()) break;
}
if(dal>=s){
cB = dal;
bylo = true;
}
if(i<inp.size()){
if(i!=0) wyn+=2*(inp[i].f-dal);
dal = inp[i].s;
}
if(!bylo) cA=inp[i].f;
}
int pocz=s, kon=s;
//cout<<cA<<" "<<cB<<'\n';
while(true){
//cout<<pocz<<" "<<kon<<'\n';
solve(pocz, kon, pocz, kon);
//cout<<pocz<<" "<<kon<<'\n';
if(pocz>cA&&kon<cB){
wyn+=2;
pocz--;
kon++;
}else break;
}
return wyn;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:74:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for(int i = 0; i < inp.size(); i++){
| ~~^~~~~~~~~~~~
books.cpp:78:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | if(i>=inp.size()) break;
| ~^~~~~~~~~~~~
books.cpp:84:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | if(i<inp.size()){
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
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 |
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 |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 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 |
0 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 |
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 |
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 |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 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 |
1 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 |
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 |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
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 |
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 |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 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 |
1 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 |
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 |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
208 ms |
24024 KB |
Output is correct |
31 |
Correct |
165 ms |
22112 KB |
Output is correct |
32 |
Correct |
108 ms |
19836 KB |
Output is correct |
33 |
Correct |
145 ms |
26340 KB |
Output is correct |
34 |
Correct |
131 ms |
26504 KB |
Output is correct |
35 |
Correct |
136 ms |
26964 KB |
Output is correct |
36 |
Correct |
142 ms |
26132 KB |
Output is correct |
37 |
Correct |
117 ms |
20676 KB |
Output is correct |
38 |
Correct |
107 ms |
19992 KB |
Output is correct |
39 |
Correct |
110 ms |
20024 KB |
Output is correct |
40 |
Correct |
119 ms |
20224 KB |
Output is correct |
41 |
Correct |
144 ms |
22944 KB |
Output is correct |
42 |
Correct |
122 ms |
21396 KB |
Output is correct |
43 |
Correct |
152 ms |
27684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
300 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 |
1 ms |
212 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 |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
1 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 |
308 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
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 |
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 |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 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 |
1 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 |
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 |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
208 ms |
24024 KB |
Output is correct |
31 |
Correct |
165 ms |
22112 KB |
Output is correct |
32 |
Correct |
108 ms |
19836 KB |
Output is correct |
33 |
Correct |
145 ms |
26340 KB |
Output is correct |
34 |
Correct |
131 ms |
26504 KB |
Output is correct |
35 |
Correct |
136 ms |
26964 KB |
Output is correct |
36 |
Correct |
142 ms |
26132 KB |
Output is correct |
37 |
Correct |
117 ms |
20676 KB |
Output is correct |
38 |
Correct |
107 ms |
19992 KB |
Output is correct |
39 |
Correct |
110 ms |
20024 KB |
Output is correct |
40 |
Correct |
119 ms |
20224 KB |
Output is correct |
41 |
Correct |
144 ms |
22944 KB |
Output is correct |
42 |
Correct |
122 ms |
21396 KB |
Output is correct |
43 |
Correct |
152 ms |
27684 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
304 KB |
Output is correct |
46 |
Correct |
1 ms |
300 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 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 |
304 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
1 ms |
308 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
308 KB |
Output is correct |
61 |
Correct |
1 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
212 KB |
Output is correct |
63 |
Correct |
1 ms |
212 KB |
Output is correct |
64 |
Correct |
128 ms |
30312 KB |
Output is correct |
65 |
Correct |
135 ms |
29884 KB |
Output is correct |
66 |
Correct |
121 ms |
27368 KB |
Output is correct |
67 |
Correct |
161 ms |
26976 KB |
Output is correct |
68 |
Correct |
14 ms |
3156 KB |
Output is correct |
69 |
Correct |
12 ms |
3072 KB |
Output is correct |
70 |
Correct |
13 ms |
3196 KB |
Output is correct |
71 |
Correct |
14 ms |
3256 KB |
Output is correct |
72 |
Correct |
16 ms |
3148 KB |
Output is correct |
73 |
Correct |
21 ms |
2900 KB |
Output is correct |
74 |
Correct |
174 ms |
33108 KB |
Output is correct |
75 |
Correct |
134 ms |
33248 KB |
Output is correct |
76 |
Correct |
154 ms |
34340 KB |
Output is correct |
77 |
Correct |
151 ms |
34380 KB |
Output is correct |
78 |
Correct |
159 ms |
39376 KB |
Output is correct |
79 |
Correct |
190 ms |
39260 KB |
Output is correct |
80 |
Correct |
108 ms |
26552 KB |
Output is correct |
81 |
Correct |
157 ms |
42444 KB |
Output is correct |
82 |
Correct |
168 ms |
42448 KB |
Output is correct |
83 |
Correct |
175 ms |
34340 KB |
Output is correct |
84 |
Correct |
143 ms |
33488 KB |
Output is correct |
85 |
Correct |
142 ms |
30160 KB |
Output is correct |
86 |
Correct |
172 ms |
28120 KB |
Output is correct |
87 |
Correct |
127 ms |
32724 KB |
Output is correct |
88 |
Correct |
153 ms |
38860 KB |
Output is correct |
89 |
Correct |
177 ms |
38872 KB |
Output is correct |
90 |
Correct |
130 ms |
27336 KB |
Output is correct |
91 |
Correct |
121 ms |
26724 KB |
Output is correct |
92 |
Correct |
123 ms |
26724 KB |
Output is correct |