#include <bits/stdc++.h>
#include "books.h"
using namespace std;
using LL = long long;
#ifdef LORENZO
const int N = 1000;
#else
const int N = 1e6 + 42;
#endif
int t[2*N];
void upd(int l, int r) {
for (l += N, r += N; l < r; l >>= 1, r >>= 1) {
if (l & 1) t[l++] = 1;
if (r & 1) t[--r] = 1;
}
}
int qry(int p) {
int ans = 0;
for (p += N; p > 0; p >>= 1) {
ans = max(ans, t[p]);
}
return ans == 0;
}
LL minimum_walk(vector<int> p, int s) {
int n = p.size();
LL ans = 0;
for (int i = 0; i < n; ++i) {
ans += abs(i - p[i]);
}
if (ans == 0) return ans;
int rl = s+1;
int rr = s;
vector<int> cyc(n, -1);
for (int i = 0, c = 0; i < n; ++i) {
if (cyc[i] != -1) continue;
int ii = i, l = i, r = i;
do {
cyc[ii] = c;
ii = p[ii];
l = min(l, ii);
r = max(r, ii);
} while (i != ii);
++c;
if (l != r) {
upd(l+1, r+1);
rl = min(rl, r+1);
rr = max(rr, l);
}
}
for (int i = rl; i <= rr; ++i) {
ans += 2 * qry(i);
}
int first_step = 0;
for (int d = 0;; ++d) {
if (s-d >= 0 && p[s-d] != s-d) {
first_step = d;
break;
} else if (s+d < n && p[s+d] != s+d) {
first_step = d;
break;
}
}
ans += 2 * first_step;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |