//Power Of Ninja Go
#include <bits/stdc++.h>
#ifdef atom
#include "grader.cpp"
#else
#include "books.h"
#endif
using namespace std;
typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector< ii > vii;
#define X first
#define Y second
#define pb push_back
int n;
const int maxn = 1e6+5;
vector<int> p;
int a[maxn], b[maxn];
ll dist[maxn];
bool vis[maxn];
int belong[maxn];
int cnt = 0;
void dfs(int u)
{
if(vis[u]) return;
vis[u] = true;
belong[u] = cnt;
a[cnt] = min(u, a[cnt]);
b[cnt] = max(u, b[cnt]);
dist[cnt] += abs(p[u]-u);
dfs(p[u]);
}
void change(int &l, int &r)
{
int ll = min(l, min(a[belong[l]], a[belong[r]]));
int rr = max(r, max(b[belong[l]], b[belong[r]]));
while(ll< l || r< rr)
{
if(ll< l)
{
l--;
ll = min(ll, a[belong[l]]);
rr = max(rr, b[belong[l]]);
}
else
{
r++;
ll = min(ll, a[belong[r]]);
rr = max(rr, b[belong[r]]);
}
}
}
int solve(int l, int r, int want_l, int want_r)
{
int ret = 0;
do
{
change(l, r);
//cout << l << ' ' << r << endl;
bool to_left = false;
int costL = 0;
int s_l = l, s_r = r;
while(true)
{
if(s_l<= want_l) break;
s_l--;
costL += 2;
change(s_l, s_r);
if(s_r> r)
{
to_left = true;
break;
}
}
//cout << s_l << '?' << s_r << endl;
bool to_right = false;
int costR = 0;
int k_l = l, k_r = r;
while(true)
{
if(k_r>= want_r) break;
k_r++;
costR += 2;
change(k_l, k_r);
if(k_l< l)
{
to_right = true;
break;
}
}
if(to_left && to_right)
{
ret += min(costL, costR);
}
else
{
ret += costL + costR;
}
l = min(s_l, k_l);
r = max(s_r, k_r);
}
while(want_l< l || r< want_r);
return ret;
}
long long minimum_walk(std::vector<int> P, int s)
{
p = P;
n = P.size();
for(int i = 0; i< n; i++) a[i] = 1e9;
int l = s, r = s;
ll res = 0;
for(int i = 0; i< n; i++)
{
if(vis[i]) continue;
dfs(i);
res += dist[cnt];
if(a[cnt] != b[cnt])
{
l = min(l, a[cnt]);
r = max(r, b[cnt]);
}
cnt++;
}
//don't forget when s = 0
return res+solve(s, s, l, r);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
404 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
404 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
232 ms |
27576 KB |
Output is correct |
31 |
Correct |
242 ms |
27680 KB |
Output is correct |
32 |
Correct |
170 ms |
39356 KB |
Output is correct |
33 |
Correct |
175 ms |
35704 KB |
Output is correct |
34 |
Correct |
176 ms |
35760 KB |
Output is correct |
35 |
Correct |
180 ms |
34108 KB |
Output is correct |
36 |
Correct |
170 ms |
30712 KB |
Output is correct |
37 |
Correct |
174 ms |
27992 KB |
Output is correct |
38 |
Correct |
169 ms |
27700 KB |
Output is correct |
39 |
Correct |
171 ms |
27768 KB |
Output is correct |
40 |
Correct |
180 ms |
27684 KB |
Output is correct |
41 |
Correct |
205 ms |
27640 KB |
Output is correct |
42 |
Correct |
198 ms |
27700 KB |
Output is correct |
43 |
Correct |
174 ms |
36488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
380 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
380 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
404 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
232 ms |
27576 KB |
Output is correct |
31 |
Correct |
242 ms |
27680 KB |
Output is correct |
32 |
Correct |
170 ms |
39356 KB |
Output is correct |
33 |
Correct |
175 ms |
35704 KB |
Output is correct |
34 |
Correct |
176 ms |
35760 KB |
Output is correct |
35 |
Correct |
180 ms |
34108 KB |
Output is correct |
36 |
Correct |
170 ms |
30712 KB |
Output is correct |
37 |
Correct |
174 ms |
27992 KB |
Output is correct |
38 |
Correct |
169 ms |
27700 KB |
Output is correct |
39 |
Correct |
171 ms |
27768 KB |
Output is correct |
40 |
Correct |
180 ms |
27684 KB |
Output is correct |
41 |
Correct |
205 ms |
27640 KB |
Output is correct |
42 |
Correct |
198 ms |
27700 KB |
Output is correct |
43 |
Correct |
174 ms |
36488 KB |
Output is correct |
44 |
Correct |
2 ms |
376 KB |
Output is correct |
45 |
Correct |
2 ms |
376 KB |
Output is correct |
46 |
Correct |
2 ms |
376 KB |
Output is correct |
47 |
Correct |
2 ms |
376 KB |
Output is correct |
48 |
Correct |
2 ms |
376 KB |
Output is correct |
49 |
Correct |
2 ms |
380 KB |
Output is correct |
50 |
Correct |
2 ms |
376 KB |
Output is correct |
51 |
Correct |
2 ms |
376 KB |
Output is correct |
52 |
Correct |
2 ms |
504 KB |
Output is correct |
53 |
Correct |
2 ms |
376 KB |
Output is correct |
54 |
Correct |
2 ms |
380 KB |
Output is correct |
55 |
Correct |
2 ms |
376 KB |
Output is correct |
56 |
Correct |
2 ms |
376 KB |
Output is correct |
57 |
Correct |
2 ms |
376 KB |
Output is correct |
58 |
Correct |
2 ms |
376 KB |
Output is correct |
59 |
Correct |
2 ms |
376 KB |
Output is correct |
60 |
Correct |
2 ms |
376 KB |
Output is correct |
61 |
Correct |
2 ms |
376 KB |
Output is correct |
62 |
Correct |
2 ms |
380 KB |
Output is correct |
63 |
Correct |
2 ms |
376 KB |
Output is correct |
64 |
Correct |
172 ms |
36216 KB |
Output is correct |
65 |
Correct |
169 ms |
37112 KB |
Output is correct |
66 |
Correct |
168 ms |
28024 KB |
Output is correct |
67 |
Correct |
169 ms |
27768 KB |
Output is correct |
68 |
Correct |
18 ms |
3320 KB |
Output is correct |
69 |
Correct |
18 ms |
3192 KB |
Output is correct |
70 |
Correct |
18 ms |
3448 KB |
Output is correct |
71 |
Correct |
18 ms |
3832 KB |
Output is correct |
72 |
Correct |
18 ms |
3960 KB |
Output is correct |
73 |
Correct |
19 ms |
3064 KB |
Output is correct |
74 |
Correct |
177 ms |
35704 KB |
Output is correct |
75 |
Correct |
179 ms |
35900 KB |
Output is correct |
76 |
Correct |
178 ms |
36284 KB |
Output is correct |
77 |
Correct |
175 ms |
36312 KB |
Output is correct |
78 |
Correct |
178 ms |
34992 KB |
Output is correct |
79 |
Correct |
174 ms |
35064 KB |
Output is correct |
80 |
Correct |
165 ms |
39416 KB |
Output is correct |
81 |
Correct |
169 ms |
33484 KB |
Output is correct |
82 |
Correct |
169 ms |
33536 KB |
Output is correct |
83 |
Correct |
176 ms |
32052 KB |
Output is correct |
84 |
Correct |
173 ms |
31156 KB |
Output is correct |
85 |
Correct |
168 ms |
29176 KB |
Output is correct |
86 |
Correct |
168 ms |
28280 KB |
Output is correct |
87 |
Correct |
201 ms |
37584 KB |
Output is correct |
88 |
Correct |
186 ms |
35832 KB |
Output is correct |
89 |
Correct |
178 ms |
33016 KB |
Output is correct |
90 |
Correct |
167 ms |
27900 KB |
Output is correct |
91 |
Correct |
170 ms |
27772 KB |
Output is correct |
92 |
Correct |
180 ms |
27568 KB |
Output is correct |