#include "books.h"
#include "bits/stdc++.h"
#define st first
#define nd second
#define mp make_pair
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 1e6+1;
struct DSU {
int f[N], cnt[N], l[N], r[N];
DSU() {
for(int i=0; i<N; ++i) {
f[i] = l[i] = r[i] = i;
cnt[i] = 1;
}
}
int Find(int x) {
if(f[x]==x) return x;
f[x] = Find(f[x]);
return f[x];
}
void Union(int x, int y) {
x = Find(x), y = Find(y);
if(x==y) return;
if(cnt[x] < cnt[y]) swap(x, y);
cnt[x] += cnt[y];
l[x] = min(l[x], l[y]);
r[x] = max(r[x], r[y]);
f[y] = x;
}
int getl(int x) { return l[Find(x)]; }
int getr(int x) { return r[Find(x)]; }
};
DSU dsu;
int segments[2][N];
bool overlap(int l1, int r1, int l2, int r2) {
return (l1<l2 && r1>r2) || (l1>r2 && r1<l2);
}
ll minimum_walk(vector<int> p, int s) {
int n = p.size();
ll ans = 0;
for(int i=0; i<n; ++i) {
ans += abs(p[i] - i);
dsu.Union(i, p[i]);
}
vector<int> vec;
for(int i=n-1; i>=0; --i) {
vector<int> undo;
while(vec.size() && dsu.getr(i) >= vec.back()) {
if(dsu.getl(vec.back()) < i) {
dsu.Union(i, vec.back());
}
undo.push_back(vec.back());
vec.pop_back();
}
if(s!=0) {
while(undo.size()) {
vec.push_back(undo.back());
undo.pop_back();
}
}
vec.push_back(i);
}
for(int i=0; i<n; ++i) {
segments[0][i] = segments[1][i] = i;
}
for(int i=0; i<n; ++i) {
if(dsu.Find(i)==i && dsu.getl(i)!=dsu.getr(i)) {
segments[0][dsu.getl(i)] = dsu.getr(i);
segments[1][dsu.getr(i)] = dsu.getl(i);
}
}
int le=0, ri=n-1;
while(le<s && dsu.getl(le)==dsu.getr(le)) {
le++;
}
while(ri>s && dsu.getl(ri)==dsu.getr(ri)) {
ri--;
}
int pos[2] = {dsu.getl(s), dsu.getr(s)}, move[2] = {-1, 1};
while(pos[0]>le || pos[1]<ri) {
int cost[2] = {0, 0}, lim[2] = {pos[0], pos[1]};
pos[0]--; pos[1]++;
for(int dir=0; dir<2; ++dir) {
while(pos[dir]>=le && pos[dir]<=ri && !overlap(pos[dir], segments[dir][pos[dir]], lim[0], lim[1]) ) {
cost[dir] -= abs(pos[dir] - segments[!dir][pos[dir]]);
pos[dir] = segments[!dir][pos[dir]] + move[dir];
}
}
pos[0] = max(pos[0], le), pos[1] = min(pos[1], ri);
cost[0] += lim[0] - pos[0];
cost[1] += pos[1] - lim[1];
if(segments[0][pos[0]] == pos[1]) {
ans += min(cost[0], cost[1]) * 2;
}
else {
ans += (cost[0] + cost[1]) * 2;
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
15980 KB |
Output is correct |
2 |
Correct |
11 ms |
15980 KB |
Output is correct |
3 |
Correct |
11 ms |
15980 KB |
Output is correct |
4 |
Correct |
11 ms |
15980 KB |
Output is correct |
5 |
Correct |
12 ms |
15980 KB |
Output is correct |
6 |
Correct |
11 ms |
15980 KB |
Output is correct |
7 |
Correct |
10 ms |
15980 KB |
Output is correct |
8 |
Correct |
12 ms |
16108 KB |
Output is correct |
9 |
Correct |
11 ms |
15980 KB |
Output is correct |
10 |
Correct |
11 ms |
15980 KB |
Output is correct |
11 |
Correct |
10 ms |
15980 KB |
Output is correct |
12 |
Correct |
10 ms |
15980 KB |
Output is correct |
13 |
Correct |
11 ms |
15980 KB |
Output is correct |
14 |
Correct |
10 ms |
15980 KB |
Output is correct |
15 |
Correct |
10 ms |
15980 KB |
Output is correct |
16 |
Correct |
10 ms |
15980 KB |
Output is correct |
17 |
Correct |
11 ms |
15980 KB |
Output is correct |
18 |
Correct |
11 ms |
15980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
15980 KB |
Output is correct |
2 |
Correct |
11 ms |
15980 KB |
Output is correct |
3 |
Correct |
11 ms |
15980 KB |
Output is correct |
4 |
Correct |
11 ms |
15980 KB |
Output is correct |
5 |
Correct |
12 ms |
15980 KB |
Output is correct |
6 |
Correct |
11 ms |
15980 KB |
Output is correct |
7 |
Correct |
10 ms |
15980 KB |
Output is correct |
8 |
Correct |
12 ms |
16108 KB |
Output is correct |
9 |
Correct |
11 ms |
15980 KB |
Output is correct |
10 |
Correct |
11 ms |
15980 KB |
Output is correct |
11 |
Correct |
10 ms |
15980 KB |
Output is correct |
12 |
Correct |
10 ms |
15980 KB |
Output is correct |
13 |
Correct |
11 ms |
15980 KB |
Output is correct |
14 |
Correct |
10 ms |
15980 KB |
Output is correct |
15 |
Correct |
10 ms |
15980 KB |
Output is correct |
16 |
Correct |
10 ms |
15980 KB |
Output is correct |
17 |
Correct |
11 ms |
15980 KB |
Output is correct |
18 |
Correct |
11 ms |
15980 KB |
Output is correct |
19 |
Correct |
11 ms |
15980 KB |
Output is correct |
20 |
Correct |
11 ms |
15992 KB |
Output is correct |
21 |
Correct |
11 ms |
15980 KB |
Output is correct |
22 |
Correct |
11 ms |
15980 KB |
Output is correct |
23 |
Correct |
11 ms |
15980 KB |
Output is correct |
24 |
Correct |
11 ms |
15980 KB |
Output is correct |
25 |
Correct |
11 ms |
15980 KB |
Output is correct |
26 |
Correct |
11 ms |
15980 KB |
Output is correct |
27 |
Correct |
11 ms |
15980 KB |
Output is correct |
28 |
Correct |
11 ms |
15980 KB |
Output is correct |
29 |
Correct |
11 ms |
15980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
15980 KB |
Output is correct |
2 |
Correct |
11 ms |
15980 KB |
Output is correct |
3 |
Correct |
11 ms |
15980 KB |
Output is correct |
4 |
Correct |
11 ms |
15980 KB |
Output is correct |
5 |
Correct |
12 ms |
15980 KB |
Output is correct |
6 |
Correct |
11 ms |
15980 KB |
Output is correct |
7 |
Correct |
10 ms |
15980 KB |
Output is correct |
8 |
Correct |
12 ms |
16108 KB |
Output is correct |
9 |
Correct |
11 ms |
15980 KB |
Output is correct |
10 |
Correct |
11 ms |
15980 KB |
Output is correct |
11 |
Correct |
10 ms |
15980 KB |
Output is correct |
12 |
Correct |
10 ms |
15980 KB |
Output is correct |
13 |
Correct |
11 ms |
15980 KB |
Output is correct |
14 |
Correct |
10 ms |
15980 KB |
Output is correct |
15 |
Correct |
10 ms |
15980 KB |
Output is correct |
16 |
Correct |
10 ms |
15980 KB |
Output is correct |
17 |
Correct |
11 ms |
15980 KB |
Output is correct |
18 |
Correct |
11 ms |
15980 KB |
Output is correct |
19 |
Correct |
11 ms |
15980 KB |
Output is correct |
20 |
Correct |
11 ms |
15992 KB |
Output is correct |
21 |
Correct |
11 ms |
15980 KB |
Output is correct |
22 |
Correct |
11 ms |
15980 KB |
Output is correct |
23 |
Correct |
11 ms |
15980 KB |
Output is correct |
24 |
Correct |
11 ms |
15980 KB |
Output is correct |
25 |
Correct |
11 ms |
15980 KB |
Output is correct |
26 |
Correct |
11 ms |
15980 KB |
Output is correct |
27 |
Correct |
11 ms |
15980 KB |
Output is correct |
28 |
Correct |
11 ms |
15980 KB |
Output is correct |
29 |
Correct |
11 ms |
15980 KB |
Output is correct |
30 |
Correct |
347 ms |
34540 KB |
Output is correct |
31 |
Correct |
331 ms |
34540 KB |
Output is correct |
32 |
Correct |
143 ms |
38744 KB |
Output is correct |
33 |
Correct |
174 ms |
37208 KB |
Output is correct |
34 |
Correct |
172 ms |
37080 KB |
Output is correct |
35 |
Correct |
183 ms |
36316 KB |
Output is correct |
36 |
Correct |
197 ms |
35176 KB |
Output is correct |
37 |
Correct |
207 ms |
34796 KB |
Output is correct |
38 |
Correct |
200 ms |
34412 KB |
Output is correct |
39 |
Correct |
202 ms |
34412 KB |
Output is correct |
40 |
Correct |
211 ms |
34412 KB |
Output is correct |
41 |
Correct |
254 ms |
34412 KB |
Output is correct |
42 |
Correct |
237 ms |
34412 KB |
Output is correct |
43 |
Correct |
191 ms |
34412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
15980 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3308' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
15980 KB |
Output is correct |
2 |
Correct |
11 ms |
15980 KB |
Output is correct |
3 |
Correct |
11 ms |
15980 KB |
Output is correct |
4 |
Correct |
11 ms |
15980 KB |
Output is correct |
5 |
Correct |
12 ms |
15980 KB |
Output is correct |
6 |
Correct |
11 ms |
15980 KB |
Output is correct |
7 |
Correct |
10 ms |
15980 KB |
Output is correct |
8 |
Correct |
12 ms |
16108 KB |
Output is correct |
9 |
Correct |
11 ms |
15980 KB |
Output is correct |
10 |
Correct |
11 ms |
15980 KB |
Output is correct |
11 |
Correct |
10 ms |
15980 KB |
Output is correct |
12 |
Correct |
10 ms |
15980 KB |
Output is correct |
13 |
Correct |
11 ms |
15980 KB |
Output is correct |
14 |
Correct |
10 ms |
15980 KB |
Output is correct |
15 |
Correct |
10 ms |
15980 KB |
Output is correct |
16 |
Correct |
10 ms |
15980 KB |
Output is correct |
17 |
Correct |
11 ms |
15980 KB |
Output is correct |
18 |
Correct |
11 ms |
15980 KB |
Output is correct |
19 |
Correct |
11 ms |
15980 KB |
Output is correct |
20 |
Correct |
11 ms |
15992 KB |
Output is correct |
21 |
Correct |
11 ms |
15980 KB |
Output is correct |
22 |
Correct |
11 ms |
15980 KB |
Output is correct |
23 |
Correct |
11 ms |
15980 KB |
Output is correct |
24 |
Correct |
11 ms |
15980 KB |
Output is correct |
25 |
Correct |
11 ms |
15980 KB |
Output is correct |
26 |
Correct |
11 ms |
15980 KB |
Output is correct |
27 |
Correct |
11 ms |
15980 KB |
Output is correct |
28 |
Correct |
11 ms |
15980 KB |
Output is correct |
29 |
Correct |
11 ms |
15980 KB |
Output is correct |
30 |
Correct |
347 ms |
34540 KB |
Output is correct |
31 |
Correct |
331 ms |
34540 KB |
Output is correct |
32 |
Correct |
143 ms |
38744 KB |
Output is correct |
33 |
Correct |
174 ms |
37208 KB |
Output is correct |
34 |
Correct |
172 ms |
37080 KB |
Output is correct |
35 |
Correct |
183 ms |
36316 KB |
Output is correct |
36 |
Correct |
197 ms |
35176 KB |
Output is correct |
37 |
Correct |
207 ms |
34796 KB |
Output is correct |
38 |
Correct |
200 ms |
34412 KB |
Output is correct |
39 |
Correct |
202 ms |
34412 KB |
Output is correct |
40 |
Correct |
211 ms |
34412 KB |
Output is correct |
41 |
Correct |
254 ms |
34412 KB |
Output is correct |
42 |
Correct |
237 ms |
34412 KB |
Output is correct |
43 |
Correct |
191 ms |
34412 KB |
Output is correct |
44 |
Incorrect |
11 ms |
15980 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3308' |
45 |
Halted |
0 ms |
0 KB |
- |