#include "books.h"
#include <vector>
using namespace std;
#define ll long long
int n;
int p[1000005];
int cycle[1000005];
bool vis[1000005];
int minpos[1000005];
int maxpos[1000005];
int nextcycle = 0;
struct Segtree {
int l, r;
Segtree* lchild;
Segtree* rchild;
pair<int, int> bounds;
};
Segtree* root;
pair<int, int> merge_pair(pair<int, int> p1, pair<int, int> p2)
{
return make_pair(min(p1.first, p2.first), max(p1.second, p2.second));
}
Segtree* build(int l, int r)
{
Segtree* st = new Segtree;
st->l = l; st->r = r;
if (l == r) {
st->lchild = st->rchild = NULL;
st->bounds.first = minpos[l];
st->bounds.second = maxpos[l];
} else {
st->lchild = build(l, (l + r) / 2);
st->rchild = build((l + r) / 2 + 1, r);
st->bounds = merge_pair(st->lchild->bounds, st->rchild->bounds);
}
return st;
}
pair<int, int> query(Segtree* st, int l, int r)
{
if (st->r < l || r < st->l) return make_pair(n, -1);
if (l <= st->l && st->r <= r) return st->bounds;
return merge_pair(query(st->lchild, l, r), query(st->rchild, l, r));
}
pair<int, int> expand(pair<int, int> old)
{
while (true) {
pair<int, int> pair2 = query(root, old.first, old.second);
if (pair2.first == old.first && pair2.second == old.second) return pair2;
old = pair2;
}
}
ll minimum_walk(vector<int> perm, int s) {
n = perm.size();
for (int i = 0; i < n; i++) p[i] = perm[i];
for (int i = 0; i < n; i++) {
if (vis[i]) continue;
minpos[nextcycle] = n;
maxpos[nextcycle] = -1;
int cur = i;
while (!vis[cur]) {
cycle[cur] = nextcycle;
vis[cur] = true;
minpos[nextcycle] = min(minpos[nextcycle], cur);
maxpos[nextcycle] = max(maxpos[nextcycle], cur);
cur = p[cur];
}
nextcycle++;
}
//fprintf(stderr, "HERE");
root = build(0, n - 1);
pair<int, int> cur = expand(make_pair(s, s));
int rounds = 0;
while (cur.first != 0 || cur.second != n - 1) {
if (cur.first == 0) {
cur.second++; cur = expand(cur); rounds++;
} else if (cur.second == n - 1) {
cur.first--; cur = expand(cur); rounds++;
} else {
pair<int, int> left_intv = cur;
int lcount = 0;
while (left_intv.first > 0 && left_intv.second <= cur.second) {
left_intv.first--; left_intv = expand(left_intv); lcount++;
}
pair<int, int> right_intv = cur;
int rcount = 0;
while (right_intv.second < n - 1 && right_intv.first >= cur.first) {
right_intv.second++; right_intv = expand(right_intv); rcount++;
}
if (lcount < rcount) {
cur = left_intv; rounds += lcount;
} else {
cur = right_intv; rounds += rcount;
}
}
}
ll ans = rounds * 2;
for (int i = 0; i < n; i++) {
ans += (ll)max(p[i] - i, i - p[i]);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
300 KB |
Output is correct |
9 |
Incorrect |
1 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '0', found: '6' |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
300 KB |
Output is correct |
9 |
Incorrect |
1 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '0', found: '6' |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
300 KB |
Output is correct |
9 |
Incorrect |
1 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '0', found: '6' |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
424 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 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
300 KB |
Output is correct |
9 |
Incorrect |
1 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '0', found: '6' |
10 |
Halted |
0 ms |
0 KB |
- |