#include <bits/stdc++.h>
#define pii pair<int, int>
#define F first
#define S second
#include "books.h"
using namespace std;
const int MX = 1e6 + 100;
pii v[MX];
int mn[MX], mx[MX], tmpn[MX], tmpx[MX];
unordered_map<int, int> dp[MX];
int dfs(int l, int r, int a, int b) {
// cout << l << ' ' << r << ' ' << a << ' ' << b << endl;
if(l == a and r == b) return 0;
if(dp[l].find(r) != dp[l].end()) return dp[l][r];
if(l == a) return 2 + dfs(l, v[r + 1].S, a, b);
if(r == b) return 2 + dfs(v[l - 1].F, r, a, b);
int l1 = min(v[l - 1].F, l);
int r1 = max(v[l - 1].S, r);
int l2 = min(v[r + 1].F, l);
int r2 = max(v[r + 1].S, r);
return dp[l][r] = 2 + min(dfs(l1, r1, a, b), dfs(l2, r2, a, b));
}
long long minimum_walk(vector<int> p, int s) {
int x = 0, y = p.size() - 1;
while(x < s and p[x] == x) x ++;
while(y > s and p[y] == y) y --;
for(int i = x; i <= y; i ++) mn[i] = mx[i] = i;
for(int i = x; i <= y; i ++) {
mn[i] = min(mn[i], p[i]);
mx[i] = max(mx[i], p[i]);
mn[p[i]] = min(mn[p[i]], i);
mx[p[i]] = max(mx[p[i]], i);
}
long long sum = 0;
for(int i = x; i <= y; i ++) sum += abs(p[i] - i);
auto update = [&] (int p, int a) {
mn[p] = min(mn[p], mn[a]);
mx[p] = max(mx[p], mx[a]);
};
{
memcpy(tmpn, mn, sizeof(mn));
memcpy(tmpx, mx, sizeof(mx));
queue<int> a, b;
for(int i = s - 1; i >= x; i --) a.push(i);
for(int i = s + 1; i <= y; i ++) b.push(i);
for(int i = s; i <= y; i ++) {
while(1) {
if(a.size() and a.front() >= mn[i]) update(i, a.front()), a.pop();
else if(b.size() and b.front() <= mx[i]) update(i, b.front()), b.pop();
else break;
}
v[i] = {mn[i], mx[i]};
i = mx[i];
}
memcpy(mn, tmpn, sizeof(mn));
memcpy(mx, tmpx, sizeof(mx));
}
{
memcpy(tmpn, mn, sizeof(mn));
memcpy(tmpx, mx, sizeof(mx));
queue<int> a, b;
for(int i = s - 1; i >= x; i --) a.push(i);
for(int i = s + 1; i <= y; i ++) b.push(i);
for(int i = s; i >= x; i --) {
while(1) {
if(a.size() and a.front() >= mn[i]) update(i, a.front()), a.pop();
else if(b.size() and b.front() <= mx[i]) update(i, b.front()), b.pop();
else break;
}
v[i] = {mn[i], mx[i]};
i = mn[i];
}
memcpy(mn, tmpn, sizeof(mn));
memcpy(mx, tmpx, sizeof(mx));
}
for(int i = x; i <= y; i ++) {
// cout << i << ' ' << v[i].F << ' ' << v[i].S << endl;
}
// exit(0);
return sum + dfs(v[s].F, v[s].S, x, y);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
70776 KB |
Output is correct |
2 |
Correct |
76 ms |
70776 KB |
Output is correct |
3 |
Correct |
80 ms |
70780 KB |
Output is correct |
4 |
Correct |
86 ms |
70904 KB |
Output is correct |
5 |
Correct |
75 ms |
70904 KB |
Output is correct |
6 |
Correct |
89 ms |
70928 KB |
Output is correct |
7 |
Correct |
78 ms |
70748 KB |
Output is correct |
8 |
Correct |
78 ms |
70776 KB |
Output is correct |
9 |
Correct |
78 ms |
70948 KB |
Output is correct |
10 |
Correct |
74 ms |
70776 KB |
Output is correct |
11 |
Correct |
76 ms |
70776 KB |
Output is correct |
12 |
Correct |
76 ms |
70776 KB |
Output is correct |
13 |
Correct |
77 ms |
70904 KB |
Output is correct |
14 |
Correct |
79 ms |
70776 KB |
Output is correct |
15 |
Correct |
73 ms |
70920 KB |
Output is correct |
16 |
Correct |
74 ms |
70776 KB |
Output is correct |
17 |
Correct |
81 ms |
70904 KB |
Output is correct |
18 |
Correct |
83 ms |
70948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
70776 KB |
Output is correct |
2 |
Correct |
76 ms |
70776 KB |
Output is correct |
3 |
Correct |
80 ms |
70780 KB |
Output is correct |
4 |
Correct |
86 ms |
70904 KB |
Output is correct |
5 |
Correct |
75 ms |
70904 KB |
Output is correct |
6 |
Correct |
89 ms |
70928 KB |
Output is correct |
7 |
Correct |
78 ms |
70748 KB |
Output is correct |
8 |
Correct |
78 ms |
70776 KB |
Output is correct |
9 |
Correct |
78 ms |
70948 KB |
Output is correct |
10 |
Correct |
74 ms |
70776 KB |
Output is correct |
11 |
Correct |
76 ms |
70776 KB |
Output is correct |
12 |
Correct |
76 ms |
70776 KB |
Output is correct |
13 |
Correct |
77 ms |
70904 KB |
Output is correct |
14 |
Correct |
79 ms |
70776 KB |
Output is correct |
15 |
Correct |
73 ms |
70920 KB |
Output is correct |
16 |
Correct |
74 ms |
70776 KB |
Output is correct |
17 |
Correct |
81 ms |
70904 KB |
Output is correct |
18 |
Correct |
83 ms |
70948 KB |
Output is correct |
19 |
Correct |
70 ms |
70964 KB |
Output is correct |
20 |
Correct |
79 ms |
70828 KB |
Output is correct |
21 |
Correct |
91 ms |
70928 KB |
Output is correct |
22 |
Correct |
76 ms |
70780 KB |
Output is correct |
23 |
Correct |
80 ms |
70968 KB |
Output is correct |
24 |
Correct |
79 ms |
70904 KB |
Output is correct |
25 |
Correct |
71 ms |
70776 KB |
Output is correct |
26 |
Correct |
74 ms |
70776 KB |
Output is correct |
27 |
Correct |
75 ms |
70732 KB |
Output is correct |
28 |
Correct |
76 ms |
70776 KB |
Output is correct |
29 |
Correct |
73 ms |
70904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
70776 KB |
Output is correct |
2 |
Correct |
76 ms |
70776 KB |
Output is correct |
3 |
Correct |
80 ms |
70780 KB |
Output is correct |
4 |
Correct |
86 ms |
70904 KB |
Output is correct |
5 |
Correct |
75 ms |
70904 KB |
Output is correct |
6 |
Correct |
89 ms |
70928 KB |
Output is correct |
7 |
Correct |
78 ms |
70748 KB |
Output is correct |
8 |
Correct |
78 ms |
70776 KB |
Output is correct |
9 |
Correct |
78 ms |
70948 KB |
Output is correct |
10 |
Correct |
74 ms |
70776 KB |
Output is correct |
11 |
Correct |
76 ms |
70776 KB |
Output is correct |
12 |
Correct |
76 ms |
70776 KB |
Output is correct |
13 |
Correct |
77 ms |
70904 KB |
Output is correct |
14 |
Correct |
79 ms |
70776 KB |
Output is correct |
15 |
Correct |
73 ms |
70920 KB |
Output is correct |
16 |
Correct |
74 ms |
70776 KB |
Output is correct |
17 |
Correct |
81 ms |
70904 KB |
Output is correct |
18 |
Correct |
83 ms |
70948 KB |
Output is correct |
19 |
Correct |
70 ms |
70964 KB |
Output is correct |
20 |
Correct |
79 ms |
70828 KB |
Output is correct |
21 |
Correct |
91 ms |
70928 KB |
Output is correct |
22 |
Correct |
76 ms |
70780 KB |
Output is correct |
23 |
Correct |
80 ms |
70968 KB |
Output is correct |
24 |
Correct |
79 ms |
70904 KB |
Output is correct |
25 |
Correct |
71 ms |
70776 KB |
Output is correct |
26 |
Correct |
74 ms |
70776 KB |
Output is correct |
27 |
Correct |
75 ms |
70732 KB |
Output is correct |
28 |
Correct |
76 ms |
70776 KB |
Output is correct |
29 |
Correct |
73 ms |
70904 KB |
Output is correct |
30 |
Correct |
257 ms |
82880 KB |
Output is correct |
31 |
Correct |
284 ms |
82808 KB |
Output is correct |
32 |
Correct |
179 ms |
78712 KB |
Output is correct |
33 |
Correct |
287 ms |
136772 KB |
Output is correct |
34 |
Correct |
371 ms |
136828 KB |
Output is correct |
35 |
Correct |
323 ms |
122420 KB |
Output is correct |
36 |
Correct |
297 ms |
97264 KB |
Output is correct |
37 |
Correct |
259 ms |
90360 KB |
Output is correct |
38 |
Correct |
251 ms |
86016 KB |
Output is correct |
39 |
Correct |
239 ms |
85552 KB |
Output is correct |
40 |
Correct |
322 ms |
83316 KB |
Output is correct |
41 |
Correct |
282 ms |
82808 KB |
Output is correct |
42 |
Correct |
307 ms |
82936 KB |
Output is correct |
43 |
Correct |
221 ms |
82808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
75792 KB |
Output is correct |
2 |
Correct |
87 ms |
70896 KB |
Output is correct |
3 |
Correct |
84 ms |
72468 KB |
Output is correct |
4 |
Correct |
105 ms |
75108 KB |
Output is correct |
5 |
Correct |
98 ms |
74872 KB |
Output is correct |
6 |
Correct |
75 ms |
70780 KB |
Output is correct |
7 |
Correct |
77 ms |
70776 KB |
Output is correct |
8 |
Correct |
74 ms |
70960 KB |
Output is correct |
9 |
Correct |
77 ms |
70776 KB |
Output is correct |
10 |
Correct |
79 ms |
70784 KB |
Output is correct |
11 |
Correct |
80 ms |
71120 KB |
Output is correct |
12 |
Correct |
76 ms |
71160 KB |
Output is correct |
13 |
Correct |
74 ms |
70772 KB |
Output is correct |
14 |
Correct |
80 ms |
70776 KB |
Output is correct |
15 |
Correct |
81 ms |
70780 KB |
Output is correct |
16 |
Correct |
83 ms |
70776 KB |
Output is correct |
17 |
Correct |
77 ms |
70820 KB |
Output is correct |
18 |
Correct |
84 ms |
70936 KB |
Output is correct |
19 |
Correct |
88 ms |
70920 KB |
Output is correct |
20 |
Correct |
82 ms |
70776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
70776 KB |
Output is correct |
2 |
Correct |
76 ms |
70776 KB |
Output is correct |
3 |
Correct |
80 ms |
70780 KB |
Output is correct |
4 |
Correct |
86 ms |
70904 KB |
Output is correct |
5 |
Correct |
75 ms |
70904 KB |
Output is correct |
6 |
Correct |
89 ms |
70928 KB |
Output is correct |
7 |
Correct |
78 ms |
70748 KB |
Output is correct |
8 |
Correct |
78 ms |
70776 KB |
Output is correct |
9 |
Correct |
78 ms |
70948 KB |
Output is correct |
10 |
Correct |
74 ms |
70776 KB |
Output is correct |
11 |
Correct |
76 ms |
70776 KB |
Output is correct |
12 |
Correct |
76 ms |
70776 KB |
Output is correct |
13 |
Correct |
77 ms |
70904 KB |
Output is correct |
14 |
Correct |
79 ms |
70776 KB |
Output is correct |
15 |
Correct |
73 ms |
70920 KB |
Output is correct |
16 |
Correct |
74 ms |
70776 KB |
Output is correct |
17 |
Correct |
81 ms |
70904 KB |
Output is correct |
18 |
Correct |
83 ms |
70948 KB |
Output is correct |
19 |
Correct |
70 ms |
70964 KB |
Output is correct |
20 |
Correct |
79 ms |
70828 KB |
Output is correct |
21 |
Correct |
91 ms |
70928 KB |
Output is correct |
22 |
Correct |
76 ms |
70780 KB |
Output is correct |
23 |
Correct |
80 ms |
70968 KB |
Output is correct |
24 |
Correct |
79 ms |
70904 KB |
Output is correct |
25 |
Correct |
71 ms |
70776 KB |
Output is correct |
26 |
Correct |
74 ms |
70776 KB |
Output is correct |
27 |
Correct |
75 ms |
70732 KB |
Output is correct |
28 |
Correct |
76 ms |
70776 KB |
Output is correct |
29 |
Correct |
73 ms |
70904 KB |
Output is correct |
30 |
Correct |
257 ms |
82880 KB |
Output is correct |
31 |
Correct |
284 ms |
82808 KB |
Output is correct |
32 |
Correct |
179 ms |
78712 KB |
Output is correct |
33 |
Correct |
287 ms |
136772 KB |
Output is correct |
34 |
Correct |
371 ms |
136828 KB |
Output is correct |
35 |
Correct |
323 ms |
122420 KB |
Output is correct |
36 |
Correct |
297 ms |
97264 KB |
Output is correct |
37 |
Correct |
259 ms |
90360 KB |
Output is correct |
38 |
Correct |
251 ms |
86016 KB |
Output is correct |
39 |
Correct |
239 ms |
85552 KB |
Output is correct |
40 |
Correct |
322 ms |
83316 KB |
Output is correct |
41 |
Correct |
282 ms |
82808 KB |
Output is correct |
42 |
Correct |
307 ms |
82936 KB |
Output is correct |
43 |
Correct |
221 ms |
82808 KB |
Output is correct |
44 |
Correct |
110 ms |
75792 KB |
Output is correct |
45 |
Correct |
87 ms |
70896 KB |
Output is correct |
46 |
Correct |
84 ms |
72468 KB |
Output is correct |
47 |
Correct |
105 ms |
75108 KB |
Output is correct |
48 |
Correct |
98 ms |
74872 KB |
Output is correct |
49 |
Correct |
75 ms |
70780 KB |
Output is correct |
50 |
Correct |
77 ms |
70776 KB |
Output is correct |
51 |
Correct |
74 ms |
70960 KB |
Output is correct |
52 |
Correct |
77 ms |
70776 KB |
Output is correct |
53 |
Correct |
79 ms |
70784 KB |
Output is correct |
54 |
Correct |
80 ms |
71120 KB |
Output is correct |
55 |
Correct |
76 ms |
71160 KB |
Output is correct |
56 |
Correct |
74 ms |
70772 KB |
Output is correct |
57 |
Correct |
80 ms |
70776 KB |
Output is correct |
58 |
Correct |
81 ms |
70780 KB |
Output is correct |
59 |
Correct |
83 ms |
70776 KB |
Output is correct |
60 |
Correct |
77 ms |
70820 KB |
Output is correct |
61 |
Correct |
84 ms |
70936 KB |
Output is correct |
62 |
Correct |
88 ms |
70920 KB |
Output is correct |
63 |
Correct |
82 ms |
70776 KB |
Output is correct |
64 |
Correct |
582 ms |
110016 KB |
Output is correct |
65 |
Execution timed out |
2044 ms |
255416 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |