#include <bits/stdc++.h>
#include "books.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using plpii = pair<ll, pii>;
const int N = 1e6+10;
const ll INF = 1e9;
int qs[N];
ll minimum_walk(vector<int> p, int s)
{
int n = p.size();
bool ok = true;
for (int i = 0; i < n; ++i) if (p[i] != i) {
ok = false;
break;
}
if (ok) return 0;
ll dist = 0;
for (int i = 0; i < n; ++i) {
if (p[i] == -1) continue;
if (p[i] == i) continue;
int u = i;
while (p[u] != -1) {
int v = p[u];
dist += abs(u-v);
p[u] = -1;
qs[min(u, v)] += 1;
qs[max(u, v)] -= 1;
u = v;
}
}
int mn = INF;
int last = -1;
p[s] = -1;
for (int i = 0; i < n; ++i) {
if (i > 0) qs[i] += qs[i-1];
if (last != -1 && p[i] == -1) {
if (last <= s && s <= i)
mn = 0;
dist += 2*(i-last);
last = -1;
}
if (qs[i] == 0 && p[i] == -1) {
last = i;
}
if (qs[i] == 0 && (i == 0 || qs[i-1] > 0))
mn = min(mn, abs(s-i));
if (qs[i] > 0 && (i == 0 || qs[i-1] == 0))
mn = min(mn, abs(s-i));
}
dist += 2*mn;
return dist;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
356 KB |
Output is correct |
4 |
Correct |
3 ms |
432 KB |
Output is correct |
5 |
Correct |
2 ms |
432 KB |
Output is correct |
6 |
Correct |
2 ms |
516 KB |
Output is correct |
7 |
Correct |
2 ms |
516 KB |
Output is correct |
8 |
Correct |
2 ms |
516 KB |
Output is correct |
9 |
Correct |
2 ms |
560 KB |
Output is correct |
10 |
Correct |
2 ms |
560 KB |
Output is correct |
11 |
Correct |
2 ms |
560 KB |
Output is correct |
12 |
Correct |
2 ms |
560 KB |
Output is correct |
13 |
Correct |
2 ms |
560 KB |
Output is correct |
14 |
Correct |
2 ms |
560 KB |
Output is correct |
15 |
Correct |
3 ms |
560 KB |
Output is correct |
16 |
Correct |
2 ms |
560 KB |
Output is correct |
17 |
Correct |
2 ms |
560 KB |
Output is correct |
18 |
Correct |
2 ms |
560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
356 KB |
Output is correct |
4 |
Correct |
3 ms |
432 KB |
Output is correct |
5 |
Correct |
2 ms |
432 KB |
Output is correct |
6 |
Correct |
2 ms |
516 KB |
Output is correct |
7 |
Correct |
2 ms |
516 KB |
Output is correct |
8 |
Correct |
2 ms |
516 KB |
Output is correct |
9 |
Correct |
2 ms |
560 KB |
Output is correct |
10 |
Correct |
2 ms |
560 KB |
Output is correct |
11 |
Correct |
2 ms |
560 KB |
Output is correct |
12 |
Correct |
2 ms |
560 KB |
Output is correct |
13 |
Correct |
2 ms |
560 KB |
Output is correct |
14 |
Correct |
2 ms |
560 KB |
Output is correct |
15 |
Correct |
3 ms |
560 KB |
Output is correct |
16 |
Correct |
2 ms |
560 KB |
Output is correct |
17 |
Correct |
2 ms |
560 KB |
Output is correct |
18 |
Correct |
2 ms |
560 KB |
Output is correct |
19 |
Correct |
2 ms |
612 KB |
Output is correct |
20 |
Correct |
2 ms |
612 KB |
Output is correct |
21 |
Correct |
3 ms |
612 KB |
Output is correct |
22 |
Correct |
2 ms |
612 KB |
Output is correct |
23 |
Correct |
2 ms |
612 KB |
Output is correct |
24 |
Correct |
2 ms |
612 KB |
Output is correct |
25 |
Correct |
3 ms |
612 KB |
Output is correct |
26 |
Correct |
2 ms |
612 KB |
Output is correct |
27 |
Correct |
3 ms |
612 KB |
Output is correct |
28 |
Correct |
2 ms |
612 KB |
Output is correct |
29 |
Correct |
3 ms |
612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
356 KB |
Output is correct |
4 |
Correct |
3 ms |
432 KB |
Output is correct |
5 |
Correct |
2 ms |
432 KB |
Output is correct |
6 |
Correct |
2 ms |
516 KB |
Output is correct |
7 |
Correct |
2 ms |
516 KB |
Output is correct |
8 |
Correct |
2 ms |
516 KB |
Output is correct |
9 |
Correct |
2 ms |
560 KB |
Output is correct |
10 |
Correct |
2 ms |
560 KB |
Output is correct |
11 |
Correct |
2 ms |
560 KB |
Output is correct |
12 |
Correct |
2 ms |
560 KB |
Output is correct |
13 |
Correct |
2 ms |
560 KB |
Output is correct |
14 |
Correct |
2 ms |
560 KB |
Output is correct |
15 |
Correct |
3 ms |
560 KB |
Output is correct |
16 |
Correct |
2 ms |
560 KB |
Output is correct |
17 |
Correct |
2 ms |
560 KB |
Output is correct |
18 |
Correct |
2 ms |
560 KB |
Output is correct |
19 |
Correct |
2 ms |
612 KB |
Output is correct |
20 |
Correct |
2 ms |
612 KB |
Output is correct |
21 |
Correct |
3 ms |
612 KB |
Output is correct |
22 |
Correct |
2 ms |
612 KB |
Output is correct |
23 |
Correct |
2 ms |
612 KB |
Output is correct |
24 |
Correct |
2 ms |
612 KB |
Output is correct |
25 |
Correct |
3 ms |
612 KB |
Output is correct |
26 |
Correct |
2 ms |
612 KB |
Output is correct |
27 |
Correct |
3 ms |
612 KB |
Output is correct |
28 |
Correct |
2 ms |
612 KB |
Output is correct |
29 |
Correct |
3 ms |
612 KB |
Output is correct |
30 |
Correct |
292 ms |
12260 KB |
Output is correct |
31 |
Correct |
280 ms |
12388 KB |
Output is correct |
32 |
Correct |
144 ms |
12388 KB |
Output is correct |
33 |
Correct |
206 ms |
12388 KB |
Output is correct |
34 |
Correct |
165 ms |
12388 KB |
Output is correct |
35 |
Correct |
193 ms |
12448 KB |
Output is correct |
36 |
Correct |
203 ms |
12448 KB |
Output is correct |
37 |
Correct |
164 ms |
12448 KB |
Output is correct |
38 |
Correct |
168 ms |
12448 KB |
Output is correct |
39 |
Correct |
183 ms |
12464 KB |
Output is correct |
40 |
Correct |
187 ms |
12464 KB |
Output is correct |
41 |
Correct |
264 ms |
12464 KB |
Output is correct |
42 |
Correct |
233 ms |
12464 KB |
Output is correct |
43 |
Correct |
173 ms |
12464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
12464 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3594' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
356 KB |
Output is correct |
4 |
Correct |
3 ms |
432 KB |
Output is correct |
5 |
Correct |
2 ms |
432 KB |
Output is correct |
6 |
Correct |
2 ms |
516 KB |
Output is correct |
7 |
Correct |
2 ms |
516 KB |
Output is correct |
8 |
Correct |
2 ms |
516 KB |
Output is correct |
9 |
Correct |
2 ms |
560 KB |
Output is correct |
10 |
Correct |
2 ms |
560 KB |
Output is correct |
11 |
Correct |
2 ms |
560 KB |
Output is correct |
12 |
Correct |
2 ms |
560 KB |
Output is correct |
13 |
Correct |
2 ms |
560 KB |
Output is correct |
14 |
Correct |
2 ms |
560 KB |
Output is correct |
15 |
Correct |
3 ms |
560 KB |
Output is correct |
16 |
Correct |
2 ms |
560 KB |
Output is correct |
17 |
Correct |
2 ms |
560 KB |
Output is correct |
18 |
Correct |
2 ms |
560 KB |
Output is correct |
19 |
Correct |
2 ms |
612 KB |
Output is correct |
20 |
Correct |
2 ms |
612 KB |
Output is correct |
21 |
Correct |
3 ms |
612 KB |
Output is correct |
22 |
Correct |
2 ms |
612 KB |
Output is correct |
23 |
Correct |
2 ms |
612 KB |
Output is correct |
24 |
Correct |
2 ms |
612 KB |
Output is correct |
25 |
Correct |
3 ms |
612 KB |
Output is correct |
26 |
Correct |
2 ms |
612 KB |
Output is correct |
27 |
Correct |
3 ms |
612 KB |
Output is correct |
28 |
Correct |
2 ms |
612 KB |
Output is correct |
29 |
Correct |
3 ms |
612 KB |
Output is correct |
30 |
Correct |
292 ms |
12260 KB |
Output is correct |
31 |
Correct |
280 ms |
12388 KB |
Output is correct |
32 |
Correct |
144 ms |
12388 KB |
Output is correct |
33 |
Correct |
206 ms |
12388 KB |
Output is correct |
34 |
Correct |
165 ms |
12388 KB |
Output is correct |
35 |
Correct |
193 ms |
12448 KB |
Output is correct |
36 |
Correct |
203 ms |
12448 KB |
Output is correct |
37 |
Correct |
164 ms |
12448 KB |
Output is correct |
38 |
Correct |
168 ms |
12448 KB |
Output is correct |
39 |
Correct |
183 ms |
12464 KB |
Output is correct |
40 |
Correct |
187 ms |
12464 KB |
Output is correct |
41 |
Correct |
264 ms |
12464 KB |
Output is correct |
42 |
Correct |
233 ms |
12464 KB |
Output is correct |
43 |
Correct |
173 ms |
12464 KB |
Output is correct |
44 |
Incorrect |
3 ms |
12464 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3594' |
45 |
Halted |
0 ms |
0 KB |
- |