#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<vector<int>> cykle;
vector<int> lewo, prawo;
vector<int> odw(n, -1);
REP(i, n) if(odw[i] < 0){
int mini = i, maks = i;
vector<int> vec;
for(int w = i; odw[w] < 0; w = perm[w]){
odw[w] = ssize(cykle);
vec.emplace_back(w);
mini = min(mini, w);
maks = max(maks, w);
}
cykle.emplace_back(vec);
lewo.emplace_back(mini);
prawo.emplace_back(maks);
}
//~ REP(i, n) printf("%d ", odw[i]);
//~ printf("\n");
//~ for(vector<int> t : cykle){
//~ for(int i : t) printf("%d ", i);
//~ printf("\n");
//~ }
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;
}
int nast = it!=ciekawe.end() ? *it : n;
int pop = it!=ciekawe.begin() ? *prev(it) : -1;
if(nast==n || (pop>=0 && l-pop <= nast-p)) wyn += (l-pop)<<1, l = pop;
else wyn += (nast-p)<<1, p = nast;
}
return wyn;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 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 |
436 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 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 |
436 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
436 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
352 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
356 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 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 |
436 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
436 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
352 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
356 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
359 ms |
72000 KB |
Output is correct |
31 |
Correct |
361 ms |
70932 KB |
Output is correct |
32 |
Correct |
162 ms |
81532 KB |
Output is correct |
33 |
Correct |
272 ms |
89252 KB |
Output is correct |
34 |
Correct |
274 ms |
89236 KB |
Output is correct |
35 |
Correct |
313 ms |
96492 KB |
Output is correct |
36 |
Correct |
338 ms |
78836 KB |
Output is correct |
37 |
Correct |
333 ms |
71640 KB |
Output is correct |
38 |
Correct |
330 ms |
69972 KB |
Output is correct |
39 |
Correct |
348 ms |
69972 KB |
Output is correct |
40 |
Correct |
339 ms |
69840 KB |
Output is correct |
41 |
Correct |
371 ms |
70112 KB |
Output is correct |
42 |
Correct |
344 ms |
70168 KB |
Output is correct |
43 |
Correct |
273 ms |
93420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3324' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 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 |
436 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
436 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
352 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
356 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
359 ms |
72000 KB |
Output is correct |
31 |
Correct |
361 ms |
70932 KB |
Output is correct |
32 |
Correct |
162 ms |
81532 KB |
Output is correct |
33 |
Correct |
272 ms |
89252 KB |
Output is correct |
34 |
Correct |
274 ms |
89236 KB |
Output is correct |
35 |
Correct |
313 ms |
96492 KB |
Output is correct |
36 |
Correct |
338 ms |
78836 KB |
Output is correct |
37 |
Correct |
333 ms |
71640 KB |
Output is correct |
38 |
Correct |
330 ms |
69972 KB |
Output is correct |
39 |
Correct |
348 ms |
69972 KB |
Output is correct |
40 |
Correct |
339 ms |
69840 KB |
Output is correct |
41 |
Correct |
371 ms |
70112 KB |
Output is correct |
42 |
Correct |
344 ms |
70168 KB |
Output is correct |
43 |
Correct |
273 ms |
93420 KB |
Output is correct |
44 |
Incorrect |
1 ms |
344 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3324' |
45 |
Halted |
0 ms |
0 KB |
- |