#include "books.h"
#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'\n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
#include "books.h"
const int oo = 1e9;
pii d[1024][1024];
int dp[1024][1024];
int mn[1024][1024];
int mx[1024][1024];
int n;
pii f1(int l,int r) {
if (l > r)
return mp(-1,-1);
if (l == mn[l][r] && r == mx[l][r])
return d[l][r] = mp(l,r);
if (d[l][r] != mp(-1,-1))
return d[l][r];
return d[l][r] = f1(min(l,mn[l][r]),max(r,mx[l][r]));
}
int f(int l,int r) {
if (l > r)
return -1;
if (l < 0 || r >= n)
return oo;
if (!l && r == n - 1)
return dp[l][r] = 0;
if (dp[l][r] != -1)
return dp[l][r];
int L,R;
tie(L,R) = f1(l,r);
if (mp(L,R) != mp(l,r))
return dp[l][r] = f(L,R);
if (0 <= l && r < n)
return dp[l][r] = min(f(l - 1,r),f(l,r + 1)) + 2;
else
return oo;
}
long long minimum_walk(std::vector<int> p, int s) {
n = p.size();
while (!p.empty() && p.back() == n - 1)
--n,p.pop_back();
if (!n)
return 0;
ll ans = 0;
for (int i = 0;i < n;++i)
ans += abs(i - p[i]);
if (!s && n > 1000) {
int mx = 0;
for (int i = 0;i + 1 < n;++i)
smax(mx,p[i]),ans += 2 * (i == mx);
} else {
memset(dp,-1,sizeof(dp));
for (int i = 0;i < n;++i)
for (int j = 0;j < n;++j)
d[i][j] = mp(-1,-1);
vi was(n,1);
for (int i = 0;i < n;++i) {
mn[i][i] = i;
mx[i][i] = i;
for (int j = i;j < n;++j) {
if (j > i)
mn[i][j] = mn[i][j - 1],
mx[i][j] = mx[i][j - 1];
smin(mn[i][j],p[j]);
smax(mx[i][j],p[j]);
}
}
ans += f(s,s);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4472 KB |
Output is correct |
2 |
Correct |
7 ms |
4636 KB |
Output is correct |
3 |
Correct |
6 ms |
4708 KB |
Output is correct |
4 |
Correct |
5 ms |
4708 KB |
Output is correct |
5 |
Correct |
5 ms |
4708 KB |
Output is correct |
6 |
Correct |
7 ms |
4708 KB |
Output is correct |
7 |
Correct |
7 ms |
4708 KB |
Output is correct |
8 |
Correct |
7 ms |
4812 KB |
Output is correct |
9 |
Correct |
2 ms |
4812 KB |
Output is correct |
10 |
Correct |
5 ms |
4912 KB |
Output is correct |
11 |
Correct |
2 ms |
4912 KB |
Output is correct |
12 |
Correct |
5 ms |
4912 KB |
Output is correct |
13 |
Correct |
5 ms |
4912 KB |
Output is correct |
14 |
Correct |
2 ms |
4912 KB |
Output is correct |
15 |
Correct |
6 ms |
4912 KB |
Output is correct |
16 |
Correct |
7 ms |
4912 KB |
Output is correct |
17 |
Correct |
7 ms |
4912 KB |
Output is correct |
18 |
Correct |
8 ms |
4912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4472 KB |
Output is correct |
2 |
Correct |
7 ms |
4636 KB |
Output is correct |
3 |
Correct |
6 ms |
4708 KB |
Output is correct |
4 |
Correct |
5 ms |
4708 KB |
Output is correct |
5 |
Correct |
5 ms |
4708 KB |
Output is correct |
6 |
Correct |
7 ms |
4708 KB |
Output is correct |
7 |
Correct |
7 ms |
4708 KB |
Output is correct |
8 |
Correct |
7 ms |
4812 KB |
Output is correct |
9 |
Correct |
2 ms |
4812 KB |
Output is correct |
10 |
Correct |
5 ms |
4912 KB |
Output is correct |
11 |
Correct |
2 ms |
4912 KB |
Output is correct |
12 |
Correct |
5 ms |
4912 KB |
Output is correct |
13 |
Correct |
5 ms |
4912 KB |
Output is correct |
14 |
Correct |
2 ms |
4912 KB |
Output is correct |
15 |
Correct |
6 ms |
4912 KB |
Output is correct |
16 |
Correct |
7 ms |
4912 KB |
Output is correct |
17 |
Correct |
7 ms |
4912 KB |
Output is correct |
18 |
Correct |
8 ms |
4912 KB |
Output is correct |
19 |
Correct |
22 ms |
20780 KB |
Output is correct |
20 |
Correct |
21 ms |
20788 KB |
Output is correct |
21 |
Correct |
3 ms |
20788 KB |
Output is correct |
22 |
Correct |
22 ms |
20792 KB |
Output is correct |
23 |
Correct |
21 ms |
20792 KB |
Output is correct |
24 |
Correct |
22 ms |
20876 KB |
Output is correct |
25 |
Correct |
17 ms |
20876 KB |
Output is correct |
26 |
Correct |
24 ms |
20876 KB |
Output is correct |
27 |
Correct |
18 ms |
20876 KB |
Output is correct |
28 |
Correct |
22 ms |
20876 KB |
Output is correct |
29 |
Correct |
24 ms |
20876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4472 KB |
Output is correct |
2 |
Correct |
7 ms |
4636 KB |
Output is correct |
3 |
Correct |
6 ms |
4708 KB |
Output is correct |
4 |
Correct |
5 ms |
4708 KB |
Output is correct |
5 |
Correct |
5 ms |
4708 KB |
Output is correct |
6 |
Correct |
7 ms |
4708 KB |
Output is correct |
7 |
Correct |
7 ms |
4708 KB |
Output is correct |
8 |
Correct |
7 ms |
4812 KB |
Output is correct |
9 |
Correct |
2 ms |
4812 KB |
Output is correct |
10 |
Correct |
5 ms |
4912 KB |
Output is correct |
11 |
Correct |
2 ms |
4912 KB |
Output is correct |
12 |
Correct |
5 ms |
4912 KB |
Output is correct |
13 |
Correct |
5 ms |
4912 KB |
Output is correct |
14 |
Correct |
2 ms |
4912 KB |
Output is correct |
15 |
Correct |
6 ms |
4912 KB |
Output is correct |
16 |
Correct |
7 ms |
4912 KB |
Output is correct |
17 |
Correct |
7 ms |
4912 KB |
Output is correct |
18 |
Correct |
8 ms |
4912 KB |
Output is correct |
19 |
Correct |
22 ms |
20780 KB |
Output is correct |
20 |
Correct |
21 ms |
20788 KB |
Output is correct |
21 |
Correct |
3 ms |
20788 KB |
Output is correct |
22 |
Correct |
22 ms |
20792 KB |
Output is correct |
23 |
Correct |
21 ms |
20792 KB |
Output is correct |
24 |
Correct |
22 ms |
20876 KB |
Output is correct |
25 |
Correct |
17 ms |
20876 KB |
Output is correct |
26 |
Correct |
24 ms |
20876 KB |
Output is correct |
27 |
Correct |
18 ms |
20876 KB |
Output is correct |
28 |
Correct |
22 ms |
20876 KB |
Output is correct |
29 |
Correct |
24 ms |
20876 KB |
Output is correct |
30 |
Correct |
191 ms |
20876 KB |
Output is correct |
31 |
Correct |
192 ms |
21912 KB |
Output is correct |
32 |
Correct |
194 ms |
28640 KB |
Output is correct |
33 |
Correct |
174 ms |
35428 KB |
Output is correct |
34 |
Correct |
170 ms |
42224 KB |
Output is correct |
35 |
Correct |
160 ms |
48888 KB |
Output is correct |
36 |
Correct |
178 ms |
55680 KB |
Output is correct |
37 |
Correct |
191 ms |
62280 KB |
Output is correct |
38 |
Correct |
171 ms |
69012 KB |
Output is correct |
39 |
Correct |
165 ms |
75732 KB |
Output is correct |
40 |
Correct |
186 ms |
79516 KB |
Output is correct |
41 |
Correct |
189 ms |
79652 KB |
Output is correct |
42 |
Correct |
224 ms |
79716 KB |
Output is correct |
43 |
Correct |
149 ms |
79716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
91876 KB |
Output is correct |
2 |
Correct |
22 ms |
91880 KB |
Output is correct |
3 |
Correct |
34 ms |
91896 KB |
Output is correct |
4 |
Correct |
33 ms |
91896 KB |
Output is correct |
5 |
Correct |
35 ms |
91896 KB |
Output is correct |
6 |
Correct |
23 ms |
91952 KB |
Output is correct |
7 |
Correct |
21 ms |
91960 KB |
Output is correct |
8 |
Correct |
17 ms |
92092 KB |
Output is correct |
9 |
Correct |
17 ms |
92092 KB |
Output is correct |
10 |
Correct |
2 ms |
92092 KB |
Output is correct |
11 |
Correct |
19 ms |
92100 KB |
Output is correct |
12 |
Correct |
25 ms |
92140 KB |
Output is correct |
13 |
Correct |
21 ms |
92140 KB |
Output is correct |
14 |
Correct |
23 ms |
92140 KB |
Output is correct |
15 |
Correct |
20 ms |
92140 KB |
Output is correct |
16 |
Correct |
23 ms |
92140 KB |
Output is correct |
17 |
Correct |
22 ms |
92140 KB |
Output is correct |
18 |
Correct |
25 ms |
92160 KB |
Output is correct |
19 |
Correct |
22 ms |
92164 KB |
Output is correct |
20 |
Correct |
25 ms |
92164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4472 KB |
Output is correct |
2 |
Correct |
7 ms |
4636 KB |
Output is correct |
3 |
Correct |
6 ms |
4708 KB |
Output is correct |
4 |
Correct |
5 ms |
4708 KB |
Output is correct |
5 |
Correct |
5 ms |
4708 KB |
Output is correct |
6 |
Correct |
7 ms |
4708 KB |
Output is correct |
7 |
Correct |
7 ms |
4708 KB |
Output is correct |
8 |
Correct |
7 ms |
4812 KB |
Output is correct |
9 |
Correct |
2 ms |
4812 KB |
Output is correct |
10 |
Correct |
5 ms |
4912 KB |
Output is correct |
11 |
Correct |
2 ms |
4912 KB |
Output is correct |
12 |
Correct |
5 ms |
4912 KB |
Output is correct |
13 |
Correct |
5 ms |
4912 KB |
Output is correct |
14 |
Correct |
2 ms |
4912 KB |
Output is correct |
15 |
Correct |
6 ms |
4912 KB |
Output is correct |
16 |
Correct |
7 ms |
4912 KB |
Output is correct |
17 |
Correct |
7 ms |
4912 KB |
Output is correct |
18 |
Correct |
8 ms |
4912 KB |
Output is correct |
19 |
Correct |
22 ms |
20780 KB |
Output is correct |
20 |
Correct |
21 ms |
20788 KB |
Output is correct |
21 |
Correct |
3 ms |
20788 KB |
Output is correct |
22 |
Correct |
22 ms |
20792 KB |
Output is correct |
23 |
Correct |
21 ms |
20792 KB |
Output is correct |
24 |
Correct |
22 ms |
20876 KB |
Output is correct |
25 |
Correct |
17 ms |
20876 KB |
Output is correct |
26 |
Correct |
24 ms |
20876 KB |
Output is correct |
27 |
Correct |
18 ms |
20876 KB |
Output is correct |
28 |
Correct |
22 ms |
20876 KB |
Output is correct |
29 |
Correct |
24 ms |
20876 KB |
Output is correct |
30 |
Correct |
191 ms |
20876 KB |
Output is correct |
31 |
Correct |
192 ms |
21912 KB |
Output is correct |
32 |
Correct |
194 ms |
28640 KB |
Output is correct |
33 |
Correct |
174 ms |
35428 KB |
Output is correct |
34 |
Correct |
170 ms |
42224 KB |
Output is correct |
35 |
Correct |
160 ms |
48888 KB |
Output is correct |
36 |
Correct |
178 ms |
55680 KB |
Output is correct |
37 |
Correct |
191 ms |
62280 KB |
Output is correct |
38 |
Correct |
171 ms |
69012 KB |
Output is correct |
39 |
Correct |
165 ms |
75732 KB |
Output is correct |
40 |
Correct |
186 ms |
79516 KB |
Output is correct |
41 |
Correct |
189 ms |
79652 KB |
Output is correct |
42 |
Correct |
224 ms |
79716 KB |
Output is correct |
43 |
Correct |
149 ms |
79716 KB |
Output is correct |
44 |
Correct |
27 ms |
91876 KB |
Output is correct |
45 |
Correct |
22 ms |
91880 KB |
Output is correct |
46 |
Correct |
34 ms |
91896 KB |
Output is correct |
47 |
Correct |
33 ms |
91896 KB |
Output is correct |
48 |
Correct |
35 ms |
91896 KB |
Output is correct |
49 |
Correct |
23 ms |
91952 KB |
Output is correct |
50 |
Correct |
21 ms |
91960 KB |
Output is correct |
51 |
Correct |
17 ms |
92092 KB |
Output is correct |
52 |
Correct |
17 ms |
92092 KB |
Output is correct |
53 |
Correct |
2 ms |
92092 KB |
Output is correct |
54 |
Correct |
19 ms |
92100 KB |
Output is correct |
55 |
Correct |
25 ms |
92140 KB |
Output is correct |
56 |
Correct |
21 ms |
92140 KB |
Output is correct |
57 |
Correct |
23 ms |
92140 KB |
Output is correct |
58 |
Correct |
20 ms |
92140 KB |
Output is correct |
59 |
Correct |
23 ms |
92140 KB |
Output is correct |
60 |
Correct |
22 ms |
92140 KB |
Output is correct |
61 |
Correct |
25 ms |
92160 KB |
Output is correct |
62 |
Correct |
22 ms |
92164 KB |
Output is correct |
63 |
Correct |
25 ms |
92164 KB |
Output is correct |
64 |
Runtime error |
280 ms |
119180 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
65 |
Halted |
0 ms |
0 KB |
- |