//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 |
436 KB |
Output is correct |
3 |
Correct |
2 ms |
436 KB |
Output is correct |
4 |
Correct |
2 ms |
516 KB |
Output is correct |
5 |
Correct |
2 ms |
676 KB |
Output is correct |
6 |
Correct |
2 ms |
684 KB |
Output is correct |
7 |
Correct |
2 ms |
684 KB |
Output is correct |
8 |
Correct |
2 ms |
684 KB |
Output is correct |
9 |
Correct |
2 ms |
684 KB |
Output is correct |
10 |
Correct |
2 ms |
684 KB |
Output is correct |
11 |
Correct |
2 ms |
684 KB |
Output is correct |
12 |
Correct |
2 ms |
712 KB |
Output is correct |
13 |
Correct |
2 ms |
712 KB |
Output is correct |
14 |
Correct |
2 ms |
712 KB |
Output is correct |
15 |
Correct |
2 ms |
772 KB |
Output is correct |
16 |
Correct |
2 ms |
772 KB |
Output is correct |
17 |
Correct |
2 ms |
772 KB |
Output is correct |
18 |
Correct |
2 ms |
772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
436 KB |
Output is correct |
3 |
Correct |
2 ms |
436 KB |
Output is correct |
4 |
Correct |
2 ms |
516 KB |
Output is correct |
5 |
Correct |
2 ms |
676 KB |
Output is correct |
6 |
Correct |
2 ms |
684 KB |
Output is correct |
7 |
Correct |
2 ms |
684 KB |
Output is correct |
8 |
Correct |
2 ms |
684 KB |
Output is correct |
9 |
Correct |
2 ms |
684 KB |
Output is correct |
10 |
Correct |
2 ms |
684 KB |
Output is correct |
11 |
Correct |
2 ms |
684 KB |
Output is correct |
12 |
Correct |
2 ms |
712 KB |
Output is correct |
13 |
Correct |
2 ms |
712 KB |
Output is correct |
14 |
Correct |
2 ms |
712 KB |
Output is correct |
15 |
Correct |
2 ms |
772 KB |
Output is correct |
16 |
Correct |
2 ms |
772 KB |
Output is correct |
17 |
Correct |
2 ms |
772 KB |
Output is correct |
18 |
Correct |
2 ms |
772 KB |
Output is correct |
19 |
Correct |
2 ms |
772 KB |
Output is correct |
20 |
Correct |
2 ms |
772 KB |
Output is correct |
21 |
Correct |
2 ms |
772 KB |
Output is correct |
22 |
Correct |
2 ms |
772 KB |
Output is correct |
23 |
Correct |
2 ms |
772 KB |
Output is correct |
24 |
Correct |
2 ms |
772 KB |
Output is correct |
25 |
Correct |
2 ms |
772 KB |
Output is correct |
26 |
Correct |
2 ms |
772 KB |
Output is correct |
27 |
Correct |
2 ms |
772 KB |
Output is correct |
28 |
Correct |
2 ms |
772 KB |
Output is correct |
29 |
Correct |
2 ms |
772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
436 KB |
Output is correct |
3 |
Correct |
2 ms |
436 KB |
Output is correct |
4 |
Correct |
2 ms |
516 KB |
Output is correct |
5 |
Correct |
2 ms |
676 KB |
Output is correct |
6 |
Correct |
2 ms |
684 KB |
Output is correct |
7 |
Correct |
2 ms |
684 KB |
Output is correct |
8 |
Correct |
2 ms |
684 KB |
Output is correct |
9 |
Correct |
2 ms |
684 KB |
Output is correct |
10 |
Correct |
2 ms |
684 KB |
Output is correct |
11 |
Correct |
2 ms |
684 KB |
Output is correct |
12 |
Correct |
2 ms |
712 KB |
Output is correct |
13 |
Correct |
2 ms |
712 KB |
Output is correct |
14 |
Correct |
2 ms |
712 KB |
Output is correct |
15 |
Correct |
2 ms |
772 KB |
Output is correct |
16 |
Correct |
2 ms |
772 KB |
Output is correct |
17 |
Correct |
2 ms |
772 KB |
Output is correct |
18 |
Correct |
2 ms |
772 KB |
Output is correct |
19 |
Correct |
2 ms |
772 KB |
Output is correct |
20 |
Correct |
2 ms |
772 KB |
Output is correct |
21 |
Correct |
2 ms |
772 KB |
Output is correct |
22 |
Correct |
2 ms |
772 KB |
Output is correct |
23 |
Correct |
2 ms |
772 KB |
Output is correct |
24 |
Correct |
2 ms |
772 KB |
Output is correct |
25 |
Correct |
2 ms |
772 KB |
Output is correct |
26 |
Correct |
2 ms |
772 KB |
Output is correct |
27 |
Correct |
2 ms |
772 KB |
Output is correct |
28 |
Correct |
2 ms |
772 KB |
Output is correct |
29 |
Correct |
2 ms |
772 KB |
Output is correct |
30 |
Correct |
230 ms |
21284 KB |
Output is correct |
31 |
Correct |
222 ms |
21324 KB |
Output is correct |
32 |
Correct |
158 ms |
33016 KB |
Output is correct |
33 |
Correct |
170 ms |
33016 KB |
Output is correct |
34 |
Correct |
178 ms |
33016 KB |
Output is correct |
35 |
Correct |
176 ms |
33016 KB |
Output is correct |
36 |
Correct |
200 ms |
33016 KB |
Output is correct |
37 |
Correct |
184 ms |
33016 KB |
Output is correct |
38 |
Correct |
191 ms |
33016 KB |
Output is correct |
39 |
Correct |
183 ms |
33016 KB |
Output is correct |
40 |
Correct |
180 ms |
33016 KB |
Output is correct |
41 |
Correct |
207 ms |
33016 KB |
Output is correct |
42 |
Correct |
201 ms |
33016 KB |
Output is correct |
43 |
Correct |
189 ms |
33016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
33016 KB |
Output is correct |
2 |
Correct |
2 ms |
33016 KB |
Output is correct |
3 |
Correct |
2 ms |
33016 KB |
Output is correct |
4 |
Correct |
2 ms |
33016 KB |
Output is correct |
5 |
Correct |
2 ms |
33016 KB |
Output is correct |
6 |
Correct |
2 ms |
33016 KB |
Output is correct |
7 |
Correct |
2 ms |
33016 KB |
Output is correct |
8 |
Correct |
2 ms |
33016 KB |
Output is correct |
9 |
Correct |
2 ms |
33016 KB |
Output is correct |
10 |
Correct |
2 ms |
33016 KB |
Output is correct |
11 |
Correct |
2 ms |
33016 KB |
Output is correct |
12 |
Correct |
3 ms |
33016 KB |
Output is correct |
13 |
Correct |
2 ms |
33016 KB |
Output is correct |
14 |
Correct |
2 ms |
33016 KB |
Output is correct |
15 |
Correct |
2 ms |
33016 KB |
Output is correct |
16 |
Correct |
2 ms |
33016 KB |
Output is correct |
17 |
Correct |
2 ms |
33016 KB |
Output is correct |
18 |
Correct |
2 ms |
33016 KB |
Output is correct |
19 |
Correct |
2 ms |
33016 KB |
Output is correct |
20 |
Correct |
2 ms |
33016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
436 KB |
Output is correct |
3 |
Correct |
2 ms |
436 KB |
Output is correct |
4 |
Correct |
2 ms |
516 KB |
Output is correct |
5 |
Correct |
2 ms |
676 KB |
Output is correct |
6 |
Correct |
2 ms |
684 KB |
Output is correct |
7 |
Correct |
2 ms |
684 KB |
Output is correct |
8 |
Correct |
2 ms |
684 KB |
Output is correct |
9 |
Correct |
2 ms |
684 KB |
Output is correct |
10 |
Correct |
2 ms |
684 KB |
Output is correct |
11 |
Correct |
2 ms |
684 KB |
Output is correct |
12 |
Correct |
2 ms |
712 KB |
Output is correct |
13 |
Correct |
2 ms |
712 KB |
Output is correct |
14 |
Correct |
2 ms |
712 KB |
Output is correct |
15 |
Correct |
2 ms |
772 KB |
Output is correct |
16 |
Correct |
2 ms |
772 KB |
Output is correct |
17 |
Correct |
2 ms |
772 KB |
Output is correct |
18 |
Correct |
2 ms |
772 KB |
Output is correct |
19 |
Correct |
2 ms |
772 KB |
Output is correct |
20 |
Correct |
2 ms |
772 KB |
Output is correct |
21 |
Correct |
2 ms |
772 KB |
Output is correct |
22 |
Correct |
2 ms |
772 KB |
Output is correct |
23 |
Correct |
2 ms |
772 KB |
Output is correct |
24 |
Correct |
2 ms |
772 KB |
Output is correct |
25 |
Correct |
2 ms |
772 KB |
Output is correct |
26 |
Correct |
2 ms |
772 KB |
Output is correct |
27 |
Correct |
2 ms |
772 KB |
Output is correct |
28 |
Correct |
2 ms |
772 KB |
Output is correct |
29 |
Correct |
2 ms |
772 KB |
Output is correct |
30 |
Correct |
230 ms |
21284 KB |
Output is correct |
31 |
Correct |
222 ms |
21324 KB |
Output is correct |
32 |
Correct |
158 ms |
33016 KB |
Output is correct |
33 |
Correct |
170 ms |
33016 KB |
Output is correct |
34 |
Correct |
178 ms |
33016 KB |
Output is correct |
35 |
Correct |
176 ms |
33016 KB |
Output is correct |
36 |
Correct |
200 ms |
33016 KB |
Output is correct |
37 |
Correct |
184 ms |
33016 KB |
Output is correct |
38 |
Correct |
191 ms |
33016 KB |
Output is correct |
39 |
Correct |
183 ms |
33016 KB |
Output is correct |
40 |
Correct |
180 ms |
33016 KB |
Output is correct |
41 |
Correct |
207 ms |
33016 KB |
Output is correct |
42 |
Correct |
201 ms |
33016 KB |
Output is correct |
43 |
Correct |
189 ms |
33016 KB |
Output is correct |
44 |
Correct |
2 ms |
33016 KB |
Output is correct |
45 |
Correct |
2 ms |
33016 KB |
Output is correct |
46 |
Correct |
2 ms |
33016 KB |
Output is correct |
47 |
Correct |
2 ms |
33016 KB |
Output is correct |
48 |
Correct |
2 ms |
33016 KB |
Output is correct |
49 |
Correct |
2 ms |
33016 KB |
Output is correct |
50 |
Correct |
2 ms |
33016 KB |
Output is correct |
51 |
Correct |
2 ms |
33016 KB |
Output is correct |
52 |
Correct |
2 ms |
33016 KB |
Output is correct |
53 |
Correct |
2 ms |
33016 KB |
Output is correct |
54 |
Correct |
2 ms |
33016 KB |
Output is correct |
55 |
Correct |
3 ms |
33016 KB |
Output is correct |
56 |
Correct |
2 ms |
33016 KB |
Output is correct |
57 |
Correct |
2 ms |
33016 KB |
Output is correct |
58 |
Correct |
2 ms |
33016 KB |
Output is correct |
59 |
Correct |
2 ms |
33016 KB |
Output is correct |
60 |
Correct |
2 ms |
33016 KB |
Output is correct |
61 |
Correct |
2 ms |
33016 KB |
Output is correct |
62 |
Correct |
2 ms |
33016 KB |
Output is correct |
63 |
Correct |
2 ms |
33016 KB |
Output is correct |
64 |
Correct |
191 ms |
36760 KB |
Output is correct |
65 |
Correct |
274 ms |
44416 KB |
Output is correct |
66 |
Correct |
183 ms |
44416 KB |
Output is correct |
67 |
Correct |
166 ms |
48576 KB |
Output is correct |
68 |
Correct |
22 ms |
48576 KB |
Output is correct |
69 |
Correct |
18 ms |
48576 KB |
Output is correct |
70 |
Correct |
17 ms |
48576 KB |
Output is correct |
71 |
Correct |
18 ms |
48576 KB |
Output is correct |
72 |
Correct |
17 ms |
48576 KB |
Output is correct |
73 |
Correct |
18 ms |
48576 KB |
Output is correct |
74 |
Correct |
176 ms |
66848 KB |
Output is correct |
75 |
Correct |
190 ms |
73476 KB |
Output is correct |
76 |
Correct |
182 ms |
80732 KB |
Output is correct |
77 |
Correct |
215 ms |
87588 KB |
Output is correct |
78 |
Correct |
176 ms |
93052 KB |
Output is correct |
79 |
Correct |
173 ms |
99812 KB |
Output is correct |
80 |
Correct |
161 ms |
110888 KB |
Output is correct |
81 |
Correct |
170 ms |
111764 KB |
Output is correct |
82 |
Correct |
232 ms |
118512 KB |
Output is correct |
83 |
Correct |
221 ms |
123828 KB |
Output is correct |
84 |
Correct |
170 ms |
129692 KB |
Output is correct |
85 |
Correct |
172 ms |
134516 KB |
Output is correct |
86 |
Correct |
165 ms |
140240 KB |
Output is correct |
87 |
Correct |
203 ms |
156196 KB |
Output is correct |
88 |
Correct |
183 ms |
161276 KB |
Output is correct |
89 |
Correct |
175 ms |
165204 KB |
Output is correct |
90 |
Correct |
174 ms |
166764 KB |
Output is correct |
91 |
Correct |
173 ms |
173428 KB |
Output is correct |
92 |
Correct |
193 ms |
180012 KB |
Output is correct |