#include "books.h"
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxN = 1'000'000;
const int ind_R = 1'000'005;
const int ind_L = -1;
const long long INF = 1'000'000'000'000'000'000LL;
int N;
vector<int> P;
int S;
int cycle_ind = -1;
vector<int> cycle(maxN, -1);
vector<int> L(maxN, ind_R);
vector<int> R(maxN, ind_L);
long long essential_cost = 0;
void extend(int& l, int& r) //[l, r] is extended fully except for the cycles of l and r
{
int l_new = min(L[cycle[l]], L[cycle[r]]);
int r_new = max(R[cycle[l]], R[cycle[r]]);
// cerr << "ln = " << l_new << ", rn = " << r_new << '\n';
while(l != l_new || r != r_new)
{
if(l != l_new)
{
l--;
l_new = min(l_new, L[cycle[l]]);
r_new = max(r_new, R[cycle[l]]);
}
if(r != r_new)
{
r++;
l_new = min(l_new, L[cycle[r]]);
r_new = max(r_new, R[cycle[r]]);
}
}
}
long long minimum_walk(vector<int> p, int s)
{
N = p.size();
P = p;
S = s;
int solve_L = S;
int solve_R = S;
for(int i = 0; i < N; i++)
{
essential_cost += abs(P[i] - i);
if(P[i] != i)
{
solve_L = min(solve_L, i);
solve_R = max(solve_R, i);
}
if(cycle[i] != -1) continue;
cycle_ind++;
int j = i;
do
{
cycle[j] = cycle_ind;
L[ cycle[j] ] = min(L[ cycle[j] ], j);
R[ cycle[j] ] = max(R[ cycle[j] ], j);
j = P[j];
} while(j != i);
}
if(solve_L == S && solve_R == S) return 0;
// for(int i = 0; i < N; i++) cerr << cycle[i] << ' ';
// cerr << '\n';
//
// cerr << "solve range = " << solve_L << ' ' << solve_R << '\n';
long long extra_cost = 0;
int l = S;
int r = S;
// cerr << "l = " << l << ", r = " << r << '\n';
extend(l, r);
// cerr << "l = " << l << ", r = " << r << '\n';
while(l != solve_L || r != solve_R)
{
bool S_cycle_exists = 0;
long long left_cost = 0;
int new_l;
for(int i = l-1; i >= solve_L; i--)
{
if(R[ cycle[i] ] >= S)
{
S_cycle_exists = 1;
left_cost = 2*(l - i);
new_l = i;
break;
}
}
if(!S_cycle_exists) break;
long long right_cost = 0;
int new_r;
for(int i = r+1; i <= solve_R; i++)
{
if(L[ cycle[i] ] <= S)
{
right_cost = 2*(i - r);
new_r = i;
break;
}
}
extra_cost += min(left_cost, right_cost);
l = new_l;
r = new_r;
extend(l, r);
}
for(int l2 = l-1; l2 >= solve_L; l2--)
{
if(l2 >= l) continue;
if(R[ cycle[l2] ] == l2)
{
extra_cost += 2*(l - l2);
l = l2;
extend(l, r);
}
}
for(int r2 = r+1; r2 <= solve_R; r2++)
{
if(r2 <= r) continue;
if(L[ cycle[r2] ] == r2)
{
extra_cost += 2*(r2 - r);
r = r2;
extend(l, r);
}
}
return essential_cost + extra_cost;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:136:5: warning: 'new_r' may be used uninitialized in this function [-Wmaybe-uninitialized]
136 | r = new_r;
| ~~^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11980 KB |
Output is correct |
2 |
Correct |
6 ms |
12060 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
8 ms |
11980 KB |
Output is correct |
5 |
Correct |
6 ms |
11980 KB |
Output is correct |
6 |
Correct |
7 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
11980 KB |
Output is correct |
8 |
Correct |
7 ms |
11980 KB |
Output is correct |
9 |
Correct |
9 ms |
11936 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
11980 KB |
Output is correct |
12 |
Correct |
7 ms |
12056 KB |
Output is correct |
13 |
Correct |
8 ms |
11936 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
7 ms |
12052 KB |
Output is correct |
16 |
Correct |
8 ms |
11980 KB |
Output is correct |
17 |
Correct |
9 ms |
11980 KB |
Output is correct |
18 |
Correct |
7 ms |
12052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11980 KB |
Output is correct |
2 |
Correct |
6 ms |
12060 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
8 ms |
11980 KB |
Output is correct |
5 |
Correct |
6 ms |
11980 KB |
Output is correct |
6 |
Correct |
7 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
11980 KB |
Output is correct |
8 |
Correct |
7 ms |
11980 KB |
Output is correct |
9 |
Correct |
9 ms |
11936 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
11980 KB |
Output is correct |
12 |
Correct |
7 ms |
12056 KB |
Output is correct |
13 |
Correct |
8 ms |
11936 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
7 ms |
12052 KB |
Output is correct |
16 |
Correct |
8 ms |
11980 KB |
Output is correct |
17 |
Correct |
9 ms |
11980 KB |
Output is correct |
18 |
Correct |
7 ms |
12052 KB |
Output is correct |
19 |
Correct |
7 ms |
12052 KB |
Output is correct |
20 |
Correct |
7 ms |
11980 KB |
Output is correct |
21 |
Correct |
7 ms |
11980 KB |
Output is correct |
22 |
Correct |
8 ms |
12108 KB |
Output is correct |
23 |
Correct |
7 ms |
11980 KB |
Output is correct |
24 |
Correct |
7 ms |
11960 KB |
Output is correct |
25 |
Correct |
7 ms |
11980 KB |
Output is correct |
26 |
Correct |
7 ms |
11980 KB |
Output is correct |
27 |
Correct |
7 ms |
11980 KB |
Output is correct |
28 |
Correct |
7 ms |
11980 KB |
Output is correct |
29 |
Correct |
10 ms |
11996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11980 KB |
Output is correct |
2 |
Correct |
6 ms |
12060 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
8 ms |
11980 KB |
Output is correct |
5 |
Correct |
6 ms |
11980 KB |
Output is correct |
6 |
Correct |
7 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
11980 KB |
Output is correct |
8 |
Correct |
7 ms |
11980 KB |
Output is correct |
9 |
Correct |
9 ms |
11936 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
11980 KB |
Output is correct |
12 |
Correct |
7 ms |
12056 KB |
Output is correct |
13 |
Correct |
8 ms |
11936 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
7 ms |
12052 KB |
Output is correct |
16 |
Correct |
8 ms |
11980 KB |
Output is correct |
17 |
Correct |
9 ms |
11980 KB |
Output is correct |
18 |
Correct |
7 ms |
12052 KB |
Output is correct |
19 |
Correct |
7 ms |
12052 KB |
Output is correct |
20 |
Correct |
7 ms |
11980 KB |
Output is correct |
21 |
Correct |
7 ms |
11980 KB |
Output is correct |
22 |
Correct |
8 ms |
12108 KB |
Output is correct |
23 |
Correct |
7 ms |
11980 KB |
Output is correct |
24 |
Correct |
7 ms |
11960 KB |
Output is correct |
25 |
Correct |
7 ms |
11980 KB |
Output is correct |
26 |
Correct |
7 ms |
11980 KB |
Output is correct |
27 |
Correct |
7 ms |
11980 KB |
Output is correct |
28 |
Correct |
7 ms |
11980 KB |
Output is correct |
29 |
Correct |
10 ms |
11996 KB |
Output is correct |
30 |
Correct |
212 ms |
30536 KB |
Output is correct |
31 |
Correct |
224 ms |
30400 KB |
Output is correct |
32 |
Correct |
137 ms |
30408 KB |
Output is correct |
33 |
Correct |
159 ms |
30400 KB |
Output is correct |
34 |
Correct |
147 ms |
30404 KB |
Output is correct |
35 |
Correct |
149 ms |
30412 KB |
Output is correct |
36 |
Correct |
166 ms |
30404 KB |
Output is correct |
37 |
Correct |
147 ms |
30404 KB |
Output is correct |
38 |
Correct |
139 ms |
30400 KB |
Output is correct |
39 |
Correct |
141 ms |
30400 KB |
Output is correct |
40 |
Correct |
144 ms |
30404 KB |
Output is correct |
41 |
Correct |
170 ms |
30660 KB |
Output is correct |
42 |
Correct |
155 ms |
30496 KB |
Output is correct |
43 |
Correct |
139 ms |
30408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
11980 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3586' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11980 KB |
Output is correct |
2 |
Correct |
6 ms |
12060 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
8 ms |
11980 KB |
Output is correct |
5 |
Correct |
6 ms |
11980 KB |
Output is correct |
6 |
Correct |
7 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
11980 KB |
Output is correct |
8 |
Correct |
7 ms |
11980 KB |
Output is correct |
9 |
Correct |
9 ms |
11936 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
11980 KB |
Output is correct |
12 |
Correct |
7 ms |
12056 KB |
Output is correct |
13 |
Correct |
8 ms |
11936 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
7 ms |
12052 KB |
Output is correct |
16 |
Correct |
8 ms |
11980 KB |
Output is correct |
17 |
Correct |
9 ms |
11980 KB |
Output is correct |
18 |
Correct |
7 ms |
12052 KB |
Output is correct |
19 |
Correct |
7 ms |
12052 KB |
Output is correct |
20 |
Correct |
7 ms |
11980 KB |
Output is correct |
21 |
Correct |
7 ms |
11980 KB |
Output is correct |
22 |
Correct |
8 ms |
12108 KB |
Output is correct |
23 |
Correct |
7 ms |
11980 KB |
Output is correct |
24 |
Correct |
7 ms |
11960 KB |
Output is correct |
25 |
Correct |
7 ms |
11980 KB |
Output is correct |
26 |
Correct |
7 ms |
11980 KB |
Output is correct |
27 |
Correct |
7 ms |
11980 KB |
Output is correct |
28 |
Correct |
7 ms |
11980 KB |
Output is correct |
29 |
Correct |
10 ms |
11996 KB |
Output is correct |
30 |
Correct |
212 ms |
30536 KB |
Output is correct |
31 |
Correct |
224 ms |
30400 KB |
Output is correct |
32 |
Correct |
137 ms |
30408 KB |
Output is correct |
33 |
Correct |
159 ms |
30400 KB |
Output is correct |
34 |
Correct |
147 ms |
30404 KB |
Output is correct |
35 |
Correct |
149 ms |
30412 KB |
Output is correct |
36 |
Correct |
166 ms |
30404 KB |
Output is correct |
37 |
Correct |
147 ms |
30404 KB |
Output is correct |
38 |
Correct |
139 ms |
30400 KB |
Output is correct |
39 |
Correct |
141 ms |
30400 KB |
Output is correct |
40 |
Correct |
144 ms |
30404 KB |
Output is correct |
41 |
Correct |
170 ms |
30660 KB |
Output is correct |
42 |
Correct |
155 ms |
30496 KB |
Output is correct |
43 |
Correct |
139 ms |
30408 KB |
Output is correct |
44 |
Incorrect |
7 ms |
11980 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3586' |
45 |
Halted |
0 ms |
0 KB |
- |