#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 1e6 + 5, INF = 1e9;
int p[maxn];
int vis[maxn];
bool taken[maxn];
vector<pair<int,int>> vec = {{-1, -1}};
int l, r;
void expand(int L, int R) {
vector<int> add;
while (l > L) {
l--;
if (p[l]!=l) add.push_back(vis[l]);
}
while (r<R) {
r++;
if (p[r]!=r) add.push_back(vis[r]);
}
for (int i:add) expand(vec[i].first, vec[i].second);
}
int minimum_walk(vector<int32_t> P, int32_t s) {
int n = P.size();
for (int i=0;i<n;i++) p[i] = P[i];
for (int i=0;i<n;i++) vis[i] = false;
int ans = 0;
int cnt = 0;
for (int i=0;i<n;i++) if (!vis[i]) {
if (p[i]==i && i==s) {
cnt++;
vis[i] = cnt;
vec.push_back({s, s});
}
if (p[i]==i) continue;
int u = i;
pair<int,int> val = {u, u};
cnt++;
while (true) {
vis[u] = cnt;
val.first = min(u, val.first);
val.second = max(u, val.second);
int v = p[u];
ans += abs(v-u);
if (v==i) break;
u = v;
}
vec.push_back(val);
}
l = s, r = s;
expand(vec[vis[s]].first, vec[vis[s]].second);
bool done = false;
while (l>0 || r<n-1) {
for (int dist = 1; ; dist++) {
int L = l-dist, R = r + dist;
if (L<0 && R>=n) {done = true; break;}
if (L>=0 && p[L]!=L) {
ans += dist*2;
expand(L, r);
break;
}
if (R<n && p[R]!=R) {
ans += dist*2;
expand(l, R);
break;
}
}
if (done) break;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
0 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
0 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
0 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4540 KB |
Output is correct |
30 |
Correct |
99 ms |
40408 KB |
Output is correct |
31 |
Correct |
108 ms |
41404 KB |
Output is correct |
32 |
Correct |
75 ms |
31312 KB |
Output is correct |
33 |
Correct |
91 ms |
36804 KB |
Output is correct |
34 |
Correct |
93 ms |
36960 KB |
Output is correct |
35 |
Correct |
94 ms |
36544 KB |
Output is correct |
36 |
Correct |
99 ms |
36032 KB |
Output is correct |
37 |
Correct |
85 ms |
31948 KB |
Output is correct |
38 |
Correct |
84 ms |
31464 KB |
Output is correct |
39 |
Correct |
83 ms |
31572 KB |
Output is correct |
40 |
Correct |
88 ms |
32336 KB |
Output is correct |
41 |
Correct |
96 ms |
37840 KB |
Output is correct |
42 |
Correct |
101 ms |
36716 KB |
Output is correct |
43 |
Correct |
105 ms |
43200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4440 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3324' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
0 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4540 KB |
Output is correct |
30 |
Correct |
99 ms |
40408 KB |
Output is correct |
31 |
Correct |
108 ms |
41404 KB |
Output is correct |
32 |
Correct |
75 ms |
31312 KB |
Output is correct |
33 |
Correct |
91 ms |
36804 KB |
Output is correct |
34 |
Correct |
93 ms |
36960 KB |
Output is correct |
35 |
Correct |
94 ms |
36544 KB |
Output is correct |
36 |
Correct |
99 ms |
36032 KB |
Output is correct |
37 |
Correct |
85 ms |
31948 KB |
Output is correct |
38 |
Correct |
84 ms |
31464 KB |
Output is correct |
39 |
Correct |
83 ms |
31572 KB |
Output is correct |
40 |
Correct |
88 ms |
32336 KB |
Output is correct |
41 |
Correct |
96 ms |
37840 KB |
Output is correct |
42 |
Correct |
101 ms |
36716 KB |
Output is correct |
43 |
Correct |
105 ms |
43200 KB |
Output is correct |
44 |
Incorrect |
1 ms |
4440 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3324' |
45 |
Halted |
0 ms |
0 KB |
- |