#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 && r == n - 1)
return 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 |
5 ms |
4672 KB |
Output is correct |
3 |
Correct |
5 ms |
4672 KB |
Output is correct |
4 |
Correct |
5 ms |
4732 KB |
Output is correct |
5 |
Correct |
7 ms |
4732 KB |
Output is correct |
6 |
Correct |
5 ms |
4732 KB |
Output is correct |
7 |
Correct |
7 ms |
4756 KB |
Output is correct |
8 |
Correct |
6 ms |
4756 KB |
Output is correct |
9 |
Correct |
2 ms |
4756 KB |
Output is correct |
10 |
Correct |
6 ms |
4756 KB |
Output is correct |
11 |
Correct |
2 ms |
4756 KB |
Output is correct |
12 |
Correct |
7 ms |
4880 KB |
Output is correct |
13 |
Correct |
7 ms |
4880 KB |
Output is correct |
14 |
Correct |
3 ms |
4880 KB |
Output is correct |
15 |
Correct |
7 ms |
4880 KB |
Output is correct |
16 |
Correct |
7 ms |
4880 KB |
Output is correct |
17 |
Correct |
6 ms |
4880 KB |
Output is correct |
18 |
Correct |
6 ms |
4880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4472 KB |
Output is correct |
2 |
Correct |
5 ms |
4672 KB |
Output is correct |
3 |
Correct |
5 ms |
4672 KB |
Output is correct |
4 |
Correct |
5 ms |
4732 KB |
Output is correct |
5 |
Correct |
7 ms |
4732 KB |
Output is correct |
6 |
Correct |
5 ms |
4732 KB |
Output is correct |
7 |
Correct |
7 ms |
4756 KB |
Output is correct |
8 |
Correct |
6 ms |
4756 KB |
Output is correct |
9 |
Correct |
2 ms |
4756 KB |
Output is correct |
10 |
Correct |
6 ms |
4756 KB |
Output is correct |
11 |
Correct |
2 ms |
4756 KB |
Output is correct |
12 |
Correct |
7 ms |
4880 KB |
Output is correct |
13 |
Correct |
7 ms |
4880 KB |
Output is correct |
14 |
Correct |
3 ms |
4880 KB |
Output is correct |
15 |
Correct |
7 ms |
4880 KB |
Output is correct |
16 |
Correct |
7 ms |
4880 KB |
Output is correct |
17 |
Correct |
6 ms |
4880 KB |
Output is correct |
18 |
Correct |
6 ms |
4880 KB |
Output is correct |
19 |
Correct |
24 ms |
20796 KB |
Output is correct |
20 |
Correct |
22 ms |
20848 KB |
Output is correct |
21 |
Correct |
2 ms |
20848 KB |
Output is correct |
22 |
Incorrect |
23 ms |
20848 KB |
3rd lines differ - on the 1st token, expected: '2082', found: '790' |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4472 KB |
Output is correct |
2 |
Correct |
5 ms |
4672 KB |
Output is correct |
3 |
Correct |
5 ms |
4672 KB |
Output is correct |
4 |
Correct |
5 ms |
4732 KB |
Output is correct |
5 |
Correct |
7 ms |
4732 KB |
Output is correct |
6 |
Correct |
5 ms |
4732 KB |
Output is correct |
7 |
Correct |
7 ms |
4756 KB |
Output is correct |
8 |
Correct |
6 ms |
4756 KB |
Output is correct |
9 |
Correct |
2 ms |
4756 KB |
Output is correct |
10 |
Correct |
6 ms |
4756 KB |
Output is correct |
11 |
Correct |
2 ms |
4756 KB |
Output is correct |
12 |
Correct |
7 ms |
4880 KB |
Output is correct |
13 |
Correct |
7 ms |
4880 KB |
Output is correct |
14 |
Correct |
3 ms |
4880 KB |
Output is correct |
15 |
Correct |
7 ms |
4880 KB |
Output is correct |
16 |
Correct |
7 ms |
4880 KB |
Output is correct |
17 |
Correct |
6 ms |
4880 KB |
Output is correct |
18 |
Correct |
6 ms |
4880 KB |
Output is correct |
19 |
Correct |
24 ms |
20796 KB |
Output is correct |
20 |
Correct |
22 ms |
20848 KB |
Output is correct |
21 |
Correct |
2 ms |
20848 KB |
Output is correct |
22 |
Incorrect |
23 ms |
20848 KB |
3rd lines differ - on the 1st token, expected: '2082', found: '790' |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
20888 KB |
Output is correct |
2 |
Correct |
26 ms |
20888 KB |
Output is correct |
3 |
Correct |
23 ms |
20888 KB |
Output is correct |
4 |
Incorrect |
29 ms |
20888 KB |
3rd lines differ - on the 1st token, expected: '2094', found: '1324' |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4472 KB |
Output is correct |
2 |
Correct |
5 ms |
4672 KB |
Output is correct |
3 |
Correct |
5 ms |
4672 KB |
Output is correct |
4 |
Correct |
5 ms |
4732 KB |
Output is correct |
5 |
Correct |
7 ms |
4732 KB |
Output is correct |
6 |
Correct |
5 ms |
4732 KB |
Output is correct |
7 |
Correct |
7 ms |
4756 KB |
Output is correct |
8 |
Correct |
6 ms |
4756 KB |
Output is correct |
9 |
Correct |
2 ms |
4756 KB |
Output is correct |
10 |
Correct |
6 ms |
4756 KB |
Output is correct |
11 |
Correct |
2 ms |
4756 KB |
Output is correct |
12 |
Correct |
7 ms |
4880 KB |
Output is correct |
13 |
Correct |
7 ms |
4880 KB |
Output is correct |
14 |
Correct |
3 ms |
4880 KB |
Output is correct |
15 |
Correct |
7 ms |
4880 KB |
Output is correct |
16 |
Correct |
7 ms |
4880 KB |
Output is correct |
17 |
Correct |
6 ms |
4880 KB |
Output is correct |
18 |
Correct |
6 ms |
4880 KB |
Output is correct |
19 |
Correct |
24 ms |
20796 KB |
Output is correct |
20 |
Correct |
22 ms |
20848 KB |
Output is correct |
21 |
Correct |
2 ms |
20848 KB |
Output is correct |
22 |
Incorrect |
23 ms |
20848 KB |
3rd lines differ - on the 1st token, expected: '2082', found: '790' |
23 |
Halted |
0 ms |
0 KB |
- |