#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define f first
#define s second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)((x).size())
#define pb push_back
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<" "<<#__VA_ARGS__<<": ", _do(__VA_ARGS__)
template <typename T> void _do(T && x) {cerr<<x<<endl;}
template <typename T, typename ...S> void _do(T && x, S&&...y) {cerr<<x<<", "; _do(y...);}
#define IOS()
#else
#define bug(...)
#define IOS() ios::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#endif
const int maxn = 1e6+5;
bool seen[maxn];
int bg[maxn];
bool dv[maxn];
ll minimum_walk(vector<int> p, int s) {
memset(seen, 0, sizeof seen);
int n = SZ(p);
ll re= 0;
for (int i = 0; i<n; ++i) {
// --p[i];
re+= abs(i-p[i]);
}
for (int i = 0; i<n; ++i) {
if (!seen[i]) {
seen[i] = 1;
bg[i] = i;
for (int x = p[i]; x != i; x = p[x]) seen[x] = 1, bg[i] = max(bg[i], x);
for (int x = p[i]; x != i; x = p[x]) bg[x] = bg[i];
bug(i, bg[i]);
}
}
int nmx = 0;
re -= 2;
for (int i = 0; i<n; ++i) {
nmx = max(nmx, bg[i]);
if (nmx <= i) {
re += 2;
dv[i] = 1;
}
}
for (int i = 0; i<n; ++i) {
if (p[i] == i && i != s) re -= 2;
else break;
}
for (int i = n-1; i>=0; --i) {
if (p[i] == i && i != s) re -= 2;
else break;
}
return re;
}
#ifdef BALBIT
signed main(){
IOS();
bug(minimum_walk({0,3,2,1},1));
bug(minimum_walk({3,2,1,0},0));
bug(minimum_walk({1,0,3,2},0));
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1280 KB |
Output is correct |
2 |
Correct |
1 ms |
1280 KB |
Output is correct |
3 |
Correct |
1 ms |
1280 KB |
Output is correct |
4 |
Correct |
1 ms |
1280 KB |
Output is correct |
5 |
Correct |
1 ms |
1280 KB |
Output is correct |
6 |
Correct |
1 ms |
1280 KB |
Output is correct |
7 |
Correct |
1 ms |
1280 KB |
Output is correct |
8 |
Correct |
1 ms |
1280 KB |
Output is correct |
9 |
Correct |
1 ms |
1280 KB |
Output is correct |
10 |
Correct |
1 ms |
1280 KB |
Output is correct |
11 |
Correct |
1 ms |
1280 KB |
Output is correct |
12 |
Correct |
1 ms |
1280 KB |
Output is correct |
13 |
Correct |
1 ms |
1280 KB |
Output is correct |
14 |
Correct |
1 ms |
1280 KB |
Output is correct |
15 |
Correct |
1 ms |
1280 KB |
Output is correct |
16 |
Correct |
1 ms |
1280 KB |
Output is correct |
17 |
Correct |
1 ms |
1280 KB |
Output is correct |
18 |
Correct |
1 ms |
1280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1280 KB |
Output is correct |
2 |
Correct |
1 ms |
1280 KB |
Output is correct |
3 |
Correct |
1 ms |
1280 KB |
Output is correct |
4 |
Correct |
1 ms |
1280 KB |
Output is correct |
5 |
Correct |
1 ms |
1280 KB |
Output is correct |
6 |
Correct |
1 ms |
1280 KB |
Output is correct |
7 |
Correct |
1 ms |
1280 KB |
Output is correct |
8 |
Correct |
1 ms |
1280 KB |
Output is correct |
9 |
Correct |
1 ms |
1280 KB |
Output is correct |
10 |
Correct |
1 ms |
1280 KB |
Output is correct |
11 |
Correct |
1 ms |
1280 KB |
Output is correct |
12 |
Correct |
1 ms |
1280 KB |
Output is correct |
13 |
Correct |
1 ms |
1280 KB |
Output is correct |
14 |
Correct |
1 ms |
1280 KB |
Output is correct |
15 |
Correct |
1 ms |
1280 KB |
Output is correct |
16 |
Correct |
1 ms |
1280 KB |
Output is correct |
17 |
Correct |
1 ms |
1280 KB |
Output is correct |
18 |
Correct |
1 ms |
1280 KB |
Output is correct |
19 |
Correct |
1 ms |
1280 KB |
Output is correct |
20 |
Correct |
1 ms |
1280 KB |
Output is correct |
21 |
Correct |
1 ms |
1440 KB |
Output is correct |
22 |
Correct |
2 ms |
1280 KB |
Output is correct |
23 |
Correct |
1 ms |
1280 KB |
Output is correct |
24 |
Correct |
1 ms |
1280 KB |
Output is correct |
25 |
Correct |
1 ms |
1280 KB |
Output is correct |
26 |
Correct |
1 ms |
1280 KB |
Output is correct |
27 |
Correct |
2 ms |
1280 KB |
Output is correct |
28 |
Correct |
1 ms |
1280 KB |
Output is correct |
29 |
Correct |
1 ms |
1280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1280 KB |
Output is correct |
2 |
Correct |
1 ms |
1280 KB |
Output is correct |
3 |
Correct |
1 ms |
1280 KB |
Output is correct |
4 |
Correct |
1 ms |
1280 KB |
Output is correct |
5 |
Correct |
1 ms |
1280 KB |
Output is correct |
6 |
Correct |
1 ms |
1280 KB |
Output is correct |
7 |
Correct |
1 ms |
1280 KB |
Output is correct |
8 |
Correct |
1 ms |
1280 KB |
Output is correct |
9 |
Correct |
1 ms |
1280 KB |
Output is correct |
10 |
Correct |
1 ms |
1280 KB |
Output is correct |
11 |
Correct |
1 ms |
1280 KB |
Output is correct |
12 |
Correct |
1 ms |
1280 KB |
Output is correct |
13 |
Correct |
1 ms |
1280 KB |
Output is correct |
14 |
Correct |
1 ms |
1280 KB |
Output is correct |
15 |
Correct |
1 ms |
1280 KB |
Output is correct |
16 |
Correct |
1 ms |
1280 KB |
Output is correct |
17 |
Correct |
1 ms |
1280 KB |
Output is correct |
18 |
Correct |
1 ms |
1280 KB |
Output is correct |
19 |
Correct |
1 ms |
1280 KB |
Output is correct |
20 |
Correct |
1 ms |
1280 KB |
Output is correct |
21 |
Correct |
1 ms |
1440 KB |
Output is correct |
22 |
Correct |
2 ms |
1280 KB |
Output is correct |
23 |
Correct |
1 ms |
1280 KB |
Output is correct |
24 |
Correct |
1 ms |
1280 KB |
Output is correct |
25 |
Correct |
1 ms |
1280 KB |
Output is correct |
26 |
Correct |
1 ms |
1280 KB |
Output is correct |
27 |
Correct |
2 ms |
1280 KB |
Output is correct |
28 |
Correct |
1 ms |
1280 KB |
Output is correct |
29 |
Correct |
1 ms |
1280 KB |
Output is correct |
30 |
Correct |
319 ms |
19704 KB |
Output is correct |
31 |
Correct |
294 ms |
19832 KB |
Output is correct |
32 |
Correct |
166 ms |
20728 KB |
Output is correct |
33 |
Correct |
172 ms |
20728 KB |
Output is correct |
34 |
Correct |
177 ms |
20792 KB |
Output is correct |
35 |
Correct |
199 ms |
20728 KB |
Output is correct |
36 |
Correct |
179 ms |
20728 KB |
Output is correct |
37 |
Correct |
174 ms |
20796 KB |
Output is correct |
38 |
Correct |
198 ms |
20728 KB |
Output is correct |
39 |
Correct |
183 ms |
20728 KB |
Output is correct |
40 |
Correct |
207 ms |
20088 KB |
Output is correct |
41 |
Correct |
227 ms |
19832 KB |
Output is correct |
42 |
Correct |
230 ms |
19832 KB |
Output is correct |
43 |
Correct |
173 ms |
19832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1408 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1280 KB |
Output is correct |
2 |
Correct |
1 ms |
1280 KB |
Output is correct |
3 |
Correct |
1 ms |
1280 KB |
Output is correct |
4 |
Correct |
1 ms |
1280 KB |
Output is correct |
5 |
Correct |
1 ms |
1280 KB |
Output is correct |
6 |
Correct |
1 ms |
1280 KB |
Output is correct |
7 |
Correct |
1 ms |
1280 KB |
Output is correct |
8 |
Correct |
1 ms |
1280 KB |
Output is correct |
9 |
Correct |
1 ms |
1280 KB |
Output is correct |
10 |
Correct |
1 ms |
1280 KB |
Output is correct |
11 |
Correct |
1 ms |
1280 KB |
Output is correct |
12 |
Correct |
1 ms |
1280 KB |
Output is correct |
13 |
Correct |
1 ms |
1280 KB |
Output is correct |
14 |
Correct |
1 ms |
1280 KB |
Output is correct |
15 |
Correct |
1 ms |
1280 KB |
Output is correct |
16 |
Correct |
1 ms |
1280 KB |
Output is correct |
17 |
Correct |
1 ms |
1280 KB |
Output is correct |
18 |
Correct |
1 ms |
1280 KB |
Output is correct |
19 |
Correct |
1 ms |
1280 KB |
Output is correct |
20 |
Correct |
1 ms |
1280 KB |
Output is correct |
21 |
Correct |
1 ms |
1440 KB |
Output is correct |
22 |
Correct |
2 ms |
1280 KB |
Output is correct |
23 |
Correct |
1 ms |
1280 KB |
Output is correct |
24 |
Correct |
1 ms |
1280 KB |
Output is correct |
25 |
Correct |
1 ms |
1280 KB |
Output is correct |
26 |
Correct |
1 ms |
1280 KB |
Output is correct |
27 |
Correct |
2 ms |
1280 KB |
Output is correct |
28 |
Correct |
1 ms |
1280 KB |
Output is correct |
29 |
Correct |
1 ms |
1280 KB |
Output is correct |
30 |
Correct |
319 ms |
19704 KB |
Output is correct |
31 |
Correct |
294 ms |
19832 KB |
Output is correct |
32 |
Correct |
166 ms |
20728 KB |
Output is correct |
33 |
Correct |
172 ms |
20728 KB |
Output is correct |
34 |
Correct |
177 ms |
20792 KB |
Output is correct |
35 |
Correct |
199 ms |
20728 KB |
Output is correct |
36 |
Correct |
179 ms |
20728 KB |
Output is correct |
37 |
Correct |
174 ms |
20796 KB |
Output is correct |
38 |
Correct |
198 ms |
20728 KB |
Output is correct |
39 |
Correct |
183 ms |
20728 KB |
Output is correct |
40 |
Correct |
207 ms |
20088 KB |
Output is correct |
41 |
Correct |
227 ms |
19832 KB |
Output is correct |
42 |
Correct |
230 ms |
19832 KB |
Output is correct |
43 |
Correct |
173 ms |
19832 KB |
Output is correct |
44 |
Incorrect |
1 ms |
1408 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |