#include "books.h"
#include <bits/stdc++.h>
#define FOR(i,p,k) for(int i=(p);i<=(k);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) (int((x).size()))
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
ll minimum_walk(vector<int> perm, int start){
int n = ssize(perm);
ll wyn = 0ll;
REP(i, n) wyn += abs(i-perm[i]);
set<int> ciekawe;
REP(i, n) if(perm[i] != i) ciekawe.emplace(i);
vector<int> lewo, prawo;
vector<int> odw(n, -1);
REP(i, n) if(odw[i] < 0){
int mini = i, maks = i;
for(int w = i; odw[w] < 0; w = perm[w]){
odw[w] = ssize(lewo);
mini = min(mini, w);
maks = max(maks, w);
}
lewo.emplace_back(mini);
prawo.emplace_back(maks);
}
bool cokolwiek = 0;
int l = start, p = start;
while(ssize(ciekawe)){
auto it = ciekawe.lower_bound(l);
if(it != ciekawe.end() && *it <= p){
l = min(l, lewo[odw[*it]]);
p = max(p, prawo[odw[*it]]);
ciekawe.erase(it);
continue;
}
if(cokolwiek){
if(it != ciekawe.end()) wyn += (*it-p)<<1, p = prawo[odw[*it]], ciekawe.erase(it);
else wyn += (l-*prev(it))<<1, l = lewo[odw[*prev(it)]], ciekawe.erase(prev(it));
continue;
}
if(l <= *ciekawe.begin() || *prev(ciekawe.end()) <= p){
cokolwiek = 1;
continue;
}
bool git = 0;
int koszt_lewo = 0;
int nowe_lewo = l;
int iter = l;
while(iter > 0){
if(iter == nowe_lewo) --nowe_lewo, ++koszt_lewo;
--iter;
if(iter != perm[iter]){
nowe_lewo = min(nowe_lewo, lewo[odw[iter]]);
if(prawo[odw[iter]] > p){git = 1; break;}
}
}
if(!git){
cokolwiek = 1;
continue;
}
git = 0;
int koszt_prawo = 0;
int nowe_prawo = p;
iter = p;
while(iter < n-1){
if(iter == nowe_prawo) ++nowe_prawo, ++koszt_prawo;
++iter;
if(iter != perm[iter]){
nowe_prawo = max(nowe_prawo, prawo[odw[iter]]);
if(lewo[odw[iter]] < l){git = 1; break;}
}
}
if(!git){
cokolwiek = 1;
continue;
}
if(koszt_lewo < koszt_prawo) wyn += koszt_lewo<<1, l = nowe_lewo;
else wyn += koszt_prawo<<1, p = nowe_prawo;
}
return wyn;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
596 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
596 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
596 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
383 ms |
59136 KB |
Output is correct |
31 |
Correct |
349 ms |
58960 KB |
Output is correct |
32 |
Correct |
84 ms |
20560 KB |
Output is correct |
33 |
Correct |
214 ms |
43260 KB |
Output is correct |
34 |
Correct |
217 ms |
44208 KB |
Output is correct |
35 |
Correct |
269 ms |
52348 KB |
Output is correct |
36 |
Correct |
341 ms |
56636 KB |
Output is correct |
37 |
Correct |
357 ms |
59220 KB |
Output is correct |
38 |
Correct |
344 ms |
58964 KB |
Output is correct |
39 |
Correct |
331 ms |
58960 KB |
Output is correct |
40 |
Correct |
374 ms |
58964 KB |
Output is correct |
41 |
Correct |
351 ms |
58960 KB |
Output is correct |
42 |
Correct |
343 ms |
59212 KB |
Output is correct |
43 |
Correct |
209 ms |
43620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
352 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
432 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
596 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
383 ms |
59136 KB |
Output is correct |
31 |
Correct |
349 ms |
58960 KB |
Output is correct |
32 |
Correct |
84 ms |
20560 KB |
Output is correct |
33 |
Correct |
214 ms |
43260 KB |
Output is correct |
34 |
Correct |
217 ms |
44208 KB |
Output is correct |
35 |
Correct |
269 ms |
52348 KB |
Output is correct |
36 |
Correct |
341 ms |
56636 KB |
Output is correct |
37 |
Correct |
357 ms |
59220 KB |
Output is correct |
38 |
Correct |
344 ms |
58964 KB |
Output is correct |
39 |
Correct |
331 ms |
58960 KB |
Output is correct |
40 |
Correct |
374 ms |
58964 KB |
Output is correct |
41 |
Correct |
351 ms |
58960 KB |
Output is correct |
42 |
Correct |
343 ms |
59212 KB |
Output is correct |
43 |
Correct |
209 ms |
43620 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
352 KB |
Output is correct |
48 |
Correct |
1 ms |
344 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
51 |
Correct |
1 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Correct |
1 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
432 KB |
Output is correct |
61 |
Correct |
1 ms |
344 KB |
Output is correct |
62 |
Correct |
1 ms |
344 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
185 ms |
45016 KB |
Output is correct |
65 |
Correct |
159 ms |
41644 KB |
Output is correct |
66 |
Correct |
380 ms |
65828 KB |
Output is correct |
67 |
Correct |
347 ms |
65732 KB |
Output is correct |
68 |
Correct |
24 ms |
6224 KB |
Output is correct |
69 |
Correct |
26 ms |
6628 KB |
Output is correct |
70 |
Correct |
22 ms |
6100 KB |
Output is correct |
71 |
Correct |
18 ms |
5256 KB |
Output is correct |
72 |
Correct |
13 ms |
4352 KB |
Output is correct |
73 |
Correct |
28 ms |
6740 KB |
Output is correct |
74 |
Correct |
222 ms |
50100 KB |
Output is correct |
75 |
Correct |
223 ms |
50612 KB |
Output is correct |
76 |
Correct |
217 ms |
52436 KB |
Output is correct |
77 |
Correct |
245 ms |
51672 KB |
Output is correct |
78 |
Correct |
284 ms |
62972 KB |
Output is correct |
79 |
Correct |
290 ms |
61952 KB |
Output is correct |
80 |
Correct |
89 ms |
27572 KB |
Output is correct |
81 |
Correct |
364 ms |
69760 KB |
Output is correct |
82 |
Correct |
371 ms |
70012 KB |
Output is correct |
83 |
Correct |
331 ms |
62852 KB |
Output is correct |
84 |
Correct |
342 ms |
64548 KB |
Output is correct |
85 |
Correct |
350 ms |
65988 KB |
Output is correct |
86 |
Correct |
350 ms |
66068 KB |
Output is correct |
87 |
Correct |
169 ms |
40460 KB |
Output is correct |
88 |
Correct |
271 ms |
55476 KB |
Output is correct |
89 |
Correct |
322 ms |
60776 KB |
Output is correct |
90 |
Correct |
353 ms |
65872 KB |
Output is correct |
91 |
Correct |
361 ms |
65768 KB |
Output is correct |
92 |
Correct |
356 ms |
65872 KB |
Output is correct |