#include "books.h"
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 1e6+10;
int n;
int ds[maxn], dsz[maxn], dsl[maxn], dsr[maxn];
int nxr[maxn], costr[maxn];
int find(int v) {
if(v == ds[v]) return v;
return ds[v] = find(ds[v]);
}
void join(int u, int v) {
u = find(u);
v = find(v);
if(u == v) return;
if(dsz[u] < dsz[v]) swap(u,v);
dsz[u]+= dsz[v];
dsl[u] = min(dsl[u],dsl[v]);
dsr[u] = max(dsr[u],dsr[v]);
ds[v] = u;
}
int L,R;
map<int,int> dp[maxn];
int sol(int l, int r) {
if(dp[l].count(r)) return dp[l][r];
if(l == L && r == R) return dp[l][r] = 0;
dp[l][r] = inf;
if(l > L) dp[l][r] = min(dp[l][r],2+sol(dsl[find(l-1)],max(r,dsr[find(l-1)])));
if(r < R) dp[l][r] = min(dp[l][r],2*costr[r]+sol(min(l,dsl[find(nxr[r])]),dsr[find(nxr[r])]));
return dp[l][r];
}
long long minimum_walk(vector<int32_t> p, int32_t s) {
// return 0;
n = p.size();
for(int i = 0; i < n; i++) {
ds[i] = i;
dsz[i] = 1;
dsl[i] = i;
dsr[i] = i;
}
for(int i = 0; i < n; i++) {
join(i,p[i]);
}
int itl = s;
while(itl != n) {
int itr = itl;
int cur = itl;
while(cur <= itr) {
itr = max(itr,dsr[find(cur)]);
cur++;
}
for(int i = itl; i < itr; i++) {
join(i,i+1);
}
itl = itr+1;
}
int itr = s;
while(itr >= 0) {
int itl = itr;
int cur = itr;
while(cur >= itl) {
itl = min(itl,dsl[find(cur)]);
cur--;
}
for(int i = itl; i < itr; i++) {
join(i,i+1);
}
itr = itl-1;
}
L = 0;
R = n-1;
while(L < s && dsz[find(L)] == 1) L++;
while(R > s && dsz[find(R)] == 1) R--;
for(int i = R-1; i >= s; i--) {
if(true or dsl[find(i+1)] < s or dsr[find(i+1)] == R) {
nxr[i] = i+1;
costr[i] = 1;
}
else {
nxr[i] = nxr[i+1];
costr[i] = costr[i+1]+1;
}
}
int ans = 0;
for(int i = 0; i < n; i++) ans+= abs(p[i]-i);
return ans+sol(dsl[find(s)],dsr[find(s)]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
57944 KB |
Output is correct |
2 |
Correct |
13 ms |
57944 KB |
Output is correct |
3 |
Correct |
13 ms |
57944 KB |
Output is correct |
4 |
Correct |
12 ms |
58128 KB |
Output is correct |
5 |
Correct |
13 ms |
58312 KB |
Output is correct |
6 |
Correct |
12 ms |
57944 KB |
Output is correct |
7 |
Correct |
12 ms |
58028 KB |
Output is correct |
8 |
Correct |
12 ms |
57944 KB |
Output is correct |
9 |
Correct |
12 ms |
55900 KB |
Output is correct |
10 |
Correct |
13 ms |
57948 KB |
Output is correct |
11 |
Correct |
13 ms |
55900 KB |
Output is correct |
12 |
Correct |
13 ms |
58028 KB |
Output is correct |
13 |
Correct |
13 ms |
57948 KB |
Output is correct |
14 |
Correct |
12 ms |
55896 KB |
Output is correct |
15 |
Correct |
12 ms |
57948 KB |
Output is correct |
16 |
Correct |
12 ms |
57948 KB |
Output is correct |
17 |
Correct |
14 ms |
57948 KB |
Output is correct |
18 |
Correct |
12 ms |
57948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
57944 KB |
Output is correct |
2 |
Correct |
13 ms |
57944 KB |
Output is correct |
3 |
Correct |
13 ms |
57944 KB |
Output is correct |
4 |
Correct |
12 ms |
58128 KB |
Output is correct |
5 |
Correct |
13 ms |
58312 KB |
Output is correct |
6 |
Correct |
12 ms |
57944 KB |
Output is correct |
7 |
Correct |
12 ms |
58028 KB |
Output is correct |
8 |
Correct |
12 ms |
57944 KB |
Output is correct |
9 |
Correct |
12 ms |
55900 KB |
Output is correct |
10 |
Correct |
13 ms |
57948 KB |
Output is correct |
11 |
Correct |
13 ms |
55900 KB |
Output is correct |
12 |
Correct |
13 ms |
58028 KB |
Output is correct |
13 |
Correct |
13 ms |
57948 KB |
Output is correct |
14 |
Correct |
12 ms |
55896 KB |
Output is correct |
15 |
Correct |
12 ms |
57948 KB |
Output is correct |
16 |
Correct |
12 ms |
57948 KB |
Output is correct |
17 |
Correct |
14 ms |
57948 KB |
Output is correct |
18 |
Correct |
12 ms |
57948 KB |
Output is correct |
19 |
Correct |
13 ms |
57948 KB |
Output is correct |
20 |
Correct |
13 ms |
58068 KB |
Output is correct |
21 |
Correct |
12 ms |
55896 KB |
Output is correct |
22 |
Correct |
13 ms |
57948 KB |
Output is correct |
23 |
Correct |
13 ms |
58016 KB |
Output is correct |
24 |
Correct |
15 ms |
58512 KB |
Output is correct |
25 |
Correct |
14 ms |
57948 KB |
Output is correct |
26 |
Correct |
15 ms |
58060 KB |
Output is correct |
27 |
Correct |
13 ms |
57948 KB |
Output is correct |
28 |
Correct |
13 ms |
58076 KB |
Output is correct |
29 |
Correct |
13 ms |
57948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
57944 KB |
Output is correct |
2 |
Correct |
13 ms |
57944 KB |
Output is correct |
3 |
Correct |
13 ms |
57944 KB |
Output is correct |
4 |
Correct |
12 ms |
58128 KB |
Output is correct |
5 |
Correct |
13 ms |
58312 KB |
Output is correct |
6 |
Correct |
12 ms |
57944 KB |
Output is correct |
7 |
Correct |
12 ms |
58028 KB |
Output is correct |
8 |
Correct |
12 ms |
57944 KB |
Output is correct |
9 |
Correct |
12 ms |
55900 KB |
Output is correct |
10 |
Correct |
13 ms |
57948 KB |
Output is correct |
11 |
Correct |
13 ms |
55900 KB |
Output is correct |
12 |
Correct |
13 ms |
58028 KB |
Output is correct |
13 |
Correct |
13 ms |
57948 KB |
Output is correct |
14 |
Correct |
12 ms |
55896 KB |
Output is correct |
15 |
Correct |
12 ms |
57948 KB |
Output is correct |
16 |
Correct |
12 ms |
57948 KB |
Output is correct |
17 |
Correct |
14 ms |
57948 KB |
Output is correct |
18 |
Correct |
12 ms |
57948 KB |
Output is correct |
19 |
Correct |
13 ms |
57948 KB |
Output is correct |
20 |
Correct |
13 ms |
58068 KB |
Output is correct |
21 |
Correct |
12 ms |
55896 KB |
Output is correct |
22 |
Correct |
13 ms |
57948 KB |
Output is correct |
23 |
Correct |
13 ms |
58016 KB |
Output is correct |
24 |
Correct |
15 ms |
58512 KB |
Output is correct |
25 |
Correct |
14 ms |
57948 KB |
Output is correct |
26 |
Correct |
15 ms |
58060 KB |
Output is correct |
27 |
Correct |
13 ms |
57948 KB |
Output is correct |
28 |
Correct |
13 ms |
58076 KB |
Output is correct |
29 |
Correct |
13 ms |
57948 KB |
Output is correct |
30 |
Correct |
187 ms |
102160 KB |
Output is correct |
31 |
Correct |
190 ms |
102360 KB |
Output is correct |
32 |
Correct |
94 ms |
87820 KB |
Output is correct |
33 |
Correct |
526 ms |
202396 KB |
Output is correct |
34 |
Correct |
564 ms |
202248 KB |
Output is correct |
35 |
Correct |
422 ms |
173472 KB |
Output is correct |
36 |
Correct |
193 ms |
122876 KB |
Output is correct |
37 |
Correct |
124 ms |
103160 KB |
Output is correct |
38 |
Correct |
121 ms |
102156 KB |
Output is correct |
39 |
Correct |
139 ms |
102224 KB |
Output is correct |
40 |
Correct |
130 ms |
102160 KB |
Output is correct |
41 |
Correct |
140 ms |
102136 KB |
Output is correct |
42 |
Correct |
131 ms |
102156 KB |
Output is correct |
43 |
Correct |
113 ms |
102156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
64788 KB |
Output is correct |
2 |
Correct |
13 ms |
57948 KB |
Output is correct |
3 |
Correct |
21 ms |
60428 KB |
Output is correct |
4 |
Correct |
36 ms |
64080 KB |
Output is correct |
5 |
Correct |
38 ms |
64352 KB |
Output is correct |
6 |
Correct |
12 ms |
57948 KB |
Output is correct |
7 |
Correct |
11 ms |
57948 KB |
Output is correct |
8 |
Correct |
13 ms |
57948 KB |
Output is correct |
9 |
Correct |
12 ms |
57948 KB |
Output is correct |
10 |
Correct |
13 ms |
55960 KB |
Output is correct |
11 |
Correct |
14 ms |
58460 KB |
Output is correct |
12 |
Correct |
18 ms |
58548 KB |
Output is correct |
13 |
Correct |
16 ms |
57948 KB |
Output is correct |
14 |
Correct |
13 ms |
57948 KB |
Output is correct |
15 |
Correct |
12 ms |
57948 KB |
Output is correct |
16 |
Correct |
12 ms |
57948 KB |
Output is correct |
17 |
Correct |
12 ms |
58208 KB |
Output is correct |
18 |
Correct |
13 ms |
57948 KB |
Output is correct |
19 |
Correct |
12 ms |
57948 KB |
Output is correct |
20 |
Correct |
15 ms |
57948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
57944 KB |
Output is correct |
2 |
Correct |
13 ms |
57944 KB |
Output is correct |
3 |
Correct |
13 ms |
57944 KB |
Output is correct |
4 |
Correct |
12 ms |
58128 KB |
Output is correct |
5 |
Correct |
13 ms |
58312 KB |
Output is correct |
6 |
Correct |
12 ms |
57944 KB |
Output is correct |
7 |
Correct |
12 ms |
58028 KB |
Output is correct |
8 |
Correct |
12 ms |
57944 KB |
Output is correct |
9 |
Correct |
12 ms |
55900 KB |
Output is correct |
10 |
Correct |
13 ms |
57948 KB |
Output is correct |
11 |
Correct |
13 ms |
55900 KB |
Output is correct |
12 |
Correct |
13 ms |
58028 KB |
Output is correct |
13 |
Correct |
13 ms |
57948 KB |
Output is correct |
14 |
Correct |
12 ms |
55896 KB |
Output is correct |
15 |
Correct |
12 ms |
57948 KB |
Output is correct |
16 |
Correct |
12 ms |
57948 KB |
Output is correct |
17 |
Correct |
14 ms |
57948 KB |
Output is correct |
18 |
Correct |
12 ms |
57948 KB |
Output is correct |
19 |
Correct |
13 ms |
57948 KB |
Output is correct |
20 |
Correct |
13 ms |
58068 KB |
Output is correct |
21 |
Correct |
12 ms |
55896 KB |
Output is correct |
22 |
Correct |
13 ms |
57948 KB |
Output is correct |
23 |
Correct |
13 ms |
58016 KB |
Output is correct |
24 |
Correct |
15 ms |
58512 KB |
Output is correct |
25 |
Correct |
14 ms |
57948 KB |
Output is correct |
26 |
Correct |
15 ms |
58060 KB |
Output is correct |
27 |
Correct |
13 ms |
57948 KB |
Output is correct |
28 |
Correct |
13 ms |
58076 KB |
Output is correct |
29 |
Correct |
13 ms |
57948 KB |
Output is correct |
30 |
Correct |
187 ms |
102160 KB |
Output is correct |
31 |
Correct |
190 ms |
102360 KB |
Output is correct |
32 |
Correct |
94 ms |
87820 KB |
Output is correct |
33 |
Correct |
526 ms |
202396 KB |
Output is correct |
34 |
Correct |
564 ms |
202248 KB |
Output is correct |
35 |
Correct |
422 ms |
173472 KB |
Output is correct |
36 |
Correct |
193 ms |
122876 KB |
Output is correct |
37 |
Correct |
124 ms |
103160 KB |
Output is correct |
38 |
Correct |
121 ms |
102156 KB |
Output is correct |
39 |
Correct |
139 ms |
102224 KB |
Output is correct |
40 |
Correct |
130 ms |
102160 KB |
Output is correct |
41 |
Correct |
140 ms |
102136 KB |
Output is correct |
42 |
Correct |
131 ms |
102156 KB |
Output is correct |
43 |
Correct |
113 ms |
102156 KB |
Output is correct |
44 |
Correct |
46 ms |
64788 KB |
Output is correct |
45 |
Correct |
13 ms |
57948 KB |
Output is correct |
46 |
Correct |
21 ms |
60428 KB |
Output is correct |
47 |
Correct |
36 ms |
64080 KB |
Output is correct |
48 |
Correct |
38 ms |
64352 KB |
Output is correct |
49 |
Correct |
12 ms |
57948 KB |
Output is correct |
50 |
Correct |
11 ms |
57948 KB |
Output is correct |
51 |
Correct |
13 ms |
57948 KB |
Output is correct |
52 |
Correct |
12 ms |
57948 KB |
Output is correct |
53 |
Correct |
13 ms |
55960 KB |
Output is correct |
54 |
Correct |
14 ms |
58460 KB |
Output is correct |
55 |
Correct |
18 ms |
58548 KB |
Output is correct |
56 |
Correct |
16 ms |
57948 KB |
Output is correct |
57 |
Correct |
13 ms |
57948 KB |
Output is correct |
58 |
Correct |
12 ms |
57948 KB |
Output is correct |
59 |
Correct |
12 ms |
57948 KB |
Output is correct |
60 |
Correct |
12 ms |
58208 KB |
Output is correct |
61 |
Correct |
13 ms |
57948 KB |
Output is correct |
62 |
Correct |
12 ms |
57948 KB |
Output is correct |
63 |
Correct |
15 ms |
57948 KB |
Output is correct |
64 |
Correct |
420 ms |
135020 KB |
Output is correct |
65 |
Execution timed out |
2044 ms |
348392 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |