#include "books.h"
#include<bits/stdc++.h>
using namespace std;
int nr, N, BG, p[1000009], d[1000009];
set < int > S;
vector < int > v[1000009];
void add (int node, int newD)
{
if (d[node] != -1) return ;
d[node] = newD, v[newD].push_back (node), S.erase (node);
int l = node, r = p[node];
if (l > r) swap (l, r);
while (1)
{
auto it = S.lower_bound (l);
if (it == S.end () || *it > r) break;
add (*it, newD);
}
}
long long minimum_walk (vector < int > pp, int ss)
{
N = pp.size (), BG = ss + 1;
for (int i=1; i<=N; i++)
p[i] = pp[i - 1] + 1;
long long ans = 0;
int lft = 0, rgt = 0;
for (int i=1; i<=N; i++)
{
int curr = i - p[i];
if (curr < 0) curr = -curr;
ans += curr;
if (i != p[i])
{
if (lft == 0) lft = i;
rgt = i;
}
}
if (lft == 0) return ans;
lft = min (lft, BG), rgt = max (rgt, BG);
for (int i=1; i<=N; i++)
S.insert (i), d[i] = -1;
add (BG, 0);
for (int i=0; !v[i].empty (); i++)
for (auto it : v[i])
{
if (it > 1) add (it - 1, i + 1);
if (it < N) add (it + 1, i + 1);
}
int maxi = 0;
for (int i=lft; i<=rgt; i++)
maxi = max (maxi, d[i]);
ans += 2LL * maxi;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
20 ms |
23908 KB |
Output is correct |
3 |
Correct |
20 ms |
23908 KB |
Output is correct |
4 |
Correct |
20 ms |
23908 KB |
Output is correct |
5 |
Correct |
20 ms |
23908 KB |
Output is correct |
6 |
Correct |
20 ms |
23908 KB |
Output is correct |
7 |
Correct |
20 ms |
23960 KB |
Output is correct |
8 |
Correct |
20 ms |
23960 KB |
Output is correct |
9 |
Correct |
30 ms |
23960 KB |
Output is correct |
10 |
Correct |
20 ms |
23960 KB |
Output is correct |
11 |
Correct |
31 ms |
23960 KB |
Output is correct |
12 |
Correct |
20 ms |
24020 KB |
Output is correct |
13 |
Correct |
19 ms |
24020 KB |
Output is correct |
14 |
Correct |
20 ms |
24020 KB |
Output is correct |
15 |
Correct |
20 ms |
24020 KB |
Output is correct |
16 |
Correct |
20 ms |
24020 KB |
Output is correct |
17 |
Correct |
21 ms |
24020 KB |
Output is correct |
18 |
Correct |
20 ms |
24136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
20 ms |
23908 KB |
Output is correct |
3 |
Correct |
20 ms |
23908 KB |
Output is correct |
4 |
Correct |
20 ms |
23908 KB |
Output is correct |
5 |
Correct |
20 ms |
23908 KB |
Output is correct |
6 |
Correct |
20 ms |
23908 KB |
Output is correct |
7 |
Correct |
20 ms |
23960 KB |
Output is correct |
8 |
Correct |
20 ms |
23960 KB |
Output is correct |
9 |
Correct |
30 ms |
23960 KB |
Output is correct |
10 |
Correct |
20 ms |
23960 KB |
Output is correct |
11 |
Correct |
31 ms |
23960 KB |
Output is correct |
12 |
Correct |
20 ms |
24020 KB |
Output is correct |
13 |
Correct |
19 ms |
24020 KB |
Output is correct |
14 |
Correct |
20 ms |
24020 KB |
Output is correct |
15 |
Correct |
20 ms |
24020 KB |
Output is correct |
16 |
Correct |
20 ms |
24020 KB |
Output is correct |
17 |
Correct |
21 ms |
24020 KB |
Output is correct |
18 |
Correct |
20 ms |
24136 KB |
Output is correct |
19 |
Correct |
20 ms |
24140 KB |
Output is correct |
20 |
Correct |
22 ms |
24268 KB |
Output is correct |
21 |
Correct |
21 ms |
24268 KB |
Output is correct |
22 |
Correct |
21 ms |
24268 KB |
Output is correct |
23 |
Correct |
20 ms |
24268 KB |
Output is correct |
24 |
Correct |
21 ms |
24268 KB |
Output is correct |
25 |
Correct |
21 ms |
24268 KB |
Output is correct |
26 |
Correct |
21 ms |
24268 KB |
Output is correct |
27 |
Correct |
20 ms |
24268 KB |
Output is correct |
28 |
Correct |
20 ms |
24268 KB |
Output is correct |
29 |
Correct |
20 ms |
24268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
20 ms |
23908 KB |
Output is correct |
3 |
Correct |
20 ms |
23908 KB |
Output is correct |
4 |
Correct |
20 ms |
23908 KB |
Output is correct |
5 |
Correct |
20 ms |
23908 KB |
Output is correct |
6 |
Correct |
20 ms |
23908 KB |
Output is correct |
7 |
Correct |
20 ms |
23960 KB |
Output is correct |
8 |
Correct |
20 ms |
23960 KB |
Output is correct |
9 |
Correct |
30 ms |
23960 KB |
Output is correct |
10 |
Correct |
20 ms |
23960 KB |
Output is correct |
11 |
Correct |
31 ms |
23960 KB |
Output is correct |
12 |
Correct |
20 ms |
24020 KB |
Output is correct |
13 |
Correct |
19 ms |
24020 KB |
Output is correct |
14 |
Correct |
20 ms |
24020 KB |
Output is correct |
15 |
Correct |
20 ms |
24020 KB |
Output is correct |
16 |
Correct |
20 ms |
24020 KB |
Output is correct |
17 |
Correct |
21 ms |
24020 KB |
Output is correct |
18 |
Correct |
20 ms |
24136 KB |
Output is correct |
19 |
Correct |
20 ms |
24140 KB |
Output is correct |
20 |
Correct |
22 ms |
24268 KB |
Output is correct |
21 |
Correct |
21 ms |
24268 KB |
Output is correct |
22 |
Correct |
21 ms |
24268 KB |
Output is correct |
23 |
Correct |
20 ms |
24268 KB |
Output is correct |
24 |
Correct |
21 ms |
24268 KB |
Output is correct |
25 |
Correct |
21 ms |
24268 KB |
Output is correct |
26 |
Correct |
21 ms |
24268 KB |
Output is correct |
27 |
Correct |
20 ms |
24268 KB |
Output is correct |
28 |
Correct |
20 ms |
24268 KB |
Output is correct |
29 |
Correct |
20 ms |
24268 KB |
Output is correct |
30 |
Correct |
850 ms |
110264 KB |
Output is correct |
31 |
Correct |
818 ms |
110264 KB |
Output is correct |
32 |
Correct |
160 ms |
110264 KB |
Output is correct |
33 |
Correct |
900 ms |
110264 KB |
Output is correct |
34 |
Correct |
864 ms |
110264 KB |
Output is correct |
35 |
Correct |
897 ms |
110264 KB |
Output is correct |
36 |
Correct |
880 ms |
110264 KB |
Output is correct |
37 |
Correct |
856 ms |
110264 KB |
Output is correct |
38 |
Correct |
845 ms |
110264 KB |
Output is correct |
39 |
Correct |
826 ms |
110264 KB |
Output is correct |
40 |
Correct |
837 ms |
110264 KB |
Output is correct |
41 |
Correct |
869 ms |
110264 KB |
Output is correct |
42 |
Correct |
846 ms |
110264 KB |
Output is correct |
43 |
Correct |
836 ms |
110264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
110264 KB |
Output is correct |
2 |
Correct |
21 ms |
110264 KB |
Output is correct |
3 |
Correct |
21 ms |
110264 KB |
Output is correct |
4 |
Incorrect |
23 ms |
110264 KB |
3rd lines differ - on the 1st token, expected: '2094', found: '1592' |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
20 ms |
23908 KB |
Output is correct |
3 |
Correct |
20 ms |
23908 KB |
Output is correct |
4 |
Correct |
20 ms |
23908 KB |
Output is correct |
5 |
Correct |
20 ms |
23908 KB |
Output is correct |
6 |
Correct |
20 ms |
23908 KB |
Output is correct |
7 |
Correct |
20 ms |
23960 KB |
Output is correct |
8 |
Correct |
20 ms |
23960 KB |
Output is correct |
9 |
Correct |
30 ms |
23960 KB |
Output is correct |
10 |
Correct |
20 ms |
23960 KB |
Output is correct |
11 |
Correct |
31 ms |
23960 KB |
Output is correct |
12 |
Correct |
20 ms |
24020 KB |
Output is correct |
13 |
Correct |
19 ms |
24020 KB |
Output is correct |
14 |
Correct |
20 ms |
24020 KB |
Output is correct |
15 |
Correct |
20 ms |
24020 KB |
Output is correct |
16 |
Correct |
20 ms |
24020 KB |
Output is correct |
17 |
Correct |
21 ms |
24020 KB |
Output is correct |
18 |
Correct |
20 ms |
24136 KB |
Output is correct |
19 |
Correct |
20 ms |
24140 KB |
Output is correct |
20 |
Correct |
22 ms |
24268 KB |
Output is correct |
21 |
Correct |
21 ms |
24268 KB |
Output is correct |
22 |
Correct |
21 ms |
24268 KB |
Output is correct |
23 |
Correct |
20 ms |
24268 KB |
Output is correct |
24 |
Correct |
21 ms |
24268 KB |
Output is correct |
25 |
Correct |
21 ms |
24268 KB |
Output is correct |
26 |
Correct |
21 ms |
24268 KB |
Output is correct |
27 |
Correct |
20 ms |
24268 KB |
Output is correct |
28 |
Correct |
20 ms |
24268 KB |
Output is correct |
29 |
Correct |
20 ms |
24268 KB |
Output is correct |
30 |
Correct |
850 ms |
110264 KB |
Output is correct |
31 |
Correct |
818 ms |
110264 KB |
Output is correct |
32 |
Correct |
160 ms |
110264 KB |
Output is correct |
33 |
Correct |
900 ms |
110264 KB |
Output is correct |
34 |
Correct |
864 ms |
110264 KB |
Output is correct |
35 |
Correct |
897 ms |
110264 KB |
Output is correct |
36 |
Correct |
880 ms |
110264 KB |
Output is correct |
37 |
Correct |
856 ms |
110264 KB |
Output is correct |
38 |
Correct |
845 ms |
110264 KB |
Output is correct |
39 |
Correct |
826 ms |
110264 KB |
Output is correct |
40 |
Correct |
837 ms |
110264 KB |
Output is correct |
41 |
Correct |
869 ms |
110264 KB |
Output is correct |
42 |
Correct |
846 ms |
110264 KB |
Output is correct |
43 |
Correct |
836 ms |
110264 KB |
Output is correct |
44 |
Correct |
20 ms |
110264 KB |
Output is correct |
45 |
Correct |
21 ms |
110264 KB |
Output is correct |
46 |
Correct |
21 ms |
110264 KB |
Output is correct |
47 |
Incorrect |
23 ms |
110264 KB |
3rd lines differ - on the 1st token, expected: '2094', found: '1592' |
48 |
Halted |
0 ms |
0 KB |
- |