// In the name of Allah
#include <bits/stdc++.h>
#include "books.h"
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef complex<ld> cld;
#define all(x) (x).begin(),(x).end()
#define len(x) ((ll) (x).size())
#define F first
#define S second
#define pb push_back
#define sep ' '
#define endl '\n'
#define Mp make_pair
#define kill(x) cout << x << '\n', exit(0)
#define set_dec(x) cout << fixed << setprecision(x);
#define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e6 + 4;
const int maxs = 1e3 + 4;
const int oo = 1e9 + 7;
int n; ll res;
int A[maxn];
int dp[maxs][maxs], mn[maxs][maxs], mx[maxs][maxs];
ll minimum_walk(vector<int> p, int s) {
n = len(p); res = 0;
for (int i = 0; i < n; i++) {
A[i] = p[i];
res += abs(i - A[i]);
}
for (int l = 0; l < n; l++) {
for (int r = l; r < n; r++) {
mn[l][r] = A[r]; mx[l][r] = A[r];
if (l != r) {
mn[l][r] = min(mn[l][r], mn[l][r - 1]);
mx[l][r] = max(mx[l][r], mx[l][r - 1]);
}
}
}
int m1 = s, m2 = s;
for (int i = 0; i < n; i++) {
if (A[i] != i) {
m1 = min(m1, i); m2 = max(m2, i);
}
}
int R = m2 - m1 + 1;
for (int ln = R; ln >= 1; ln--) {
for (int l = m1, r = m1 + ln - 1; r <= m2; l++, r++) {
if (r < s || l > s) continue;
if (ln == R) dp[l][r] = 0;
else {
int lx = min(l, mn[l][r]), rx = max(r, mx[l][r]);
if (l == lx && r == rx) {
dp[l][r] = oo;
if (l - 1 >= m1) dp[l][r] = min(dp[l][r], dp[l - 1][r] + 1);
if (r + 1 <= m2) dp[l][r] = min(dp[l][r], dp[l][r + 1] + 1);
}
else {
dp[l][r] = dp[lx][rx];
}
}
}
}
res += 2 * dp[s][s];
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
428 KB |
Output is correct |
10 |
Correct |
0 ms |
508 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
428 KB |
Output is correct |
10 |
Correct |
0 ms |
508 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
5 ms |
8284 KB |
Output is correct |
20 |
Correct |
6 ms |
8128 KB |
Output is correct |
21 |
Correct |
5 ms |
8128 KB |
Output is correct |
22 |
Correct |
7 ms |
8288 KB |
Output is correct |
23 |
Correct |
5 ms |
8284 KB |
Output is correct |
24 |
Correct |
5 ms |
8284 KB |
Output is correct |
25 |
Correct |
5 ms |
8284 KB |
Output is correct |
26 |
Correct |
5 ms |
8240 KB |
Output is correct |
27 |
Correct |
6 ms |
8284 KB |
Output is correct |
28 |
Correct |
5 ms |
8340 KB |
Output is correct |
29 |
Correct |
7 ms |
8280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
428 KB |
Output is correct |
10 |
Correct |
0 ms |
508 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
5 ms |
8284 KB |
Output is correct |
20 |
Correct |
6 ms |
8128 KB |
Output is correct |
21 |
Correct |
5 ms |
8128 KB |
Output is correct |
22 |
Correct |
7 ms |
8288 KB |
Output is correct |
23 |
Correct |
5 ms |
8284 KB |
Output is correct |
24 |
Correct |
5 ms |
8284 KB |
Output is correct |
25 |
Correct |
5 ms |
8284 KB |
Output is correct |
26 |
Correct |
5 ms |
8240 KB |
Output is correct |
27 |
Correct |
6 ms |
8284 KB |
Output is correct |
28 |
Correct |
5 ms |
8340 KB |
Output is correct |
29 |
Correct |
7 ms |
8280 KB |
Output is correct |
30 |
Execution timed out |
2032 ms |
28984 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9912 KB |
Output is correct |
2 |
Correct |
5 ms |
8284 KB |
Output is correct |
3 |
Correct |
9 ms |
9736 KB |
Output is correct |
4 |
Correct |
7 ms |
10588 KB |
Output is correct |
5 |
Correct |
7 ms |
10076 KB |
Output is correct |
6 |
Correct |
6 ms |
9816 KB |
Output is correct |
7 |
Correct |
7 ms |
10076 KB |
Output is correct |
8 |
Correct |
7 ms |
10168 KB |
Output is correct |
9 |
Correct |
10 ms |
10840 KB |
Output is correct |
10 |
Correct |
5 ms |
8128 KB |
Output is correct |
11 |
Correct |
8 ms |
10076 KB |
Output is correct |
12 |
Correct |
8 ms |
10332 KB |
Output is correct |
13 |
Correct |
7 ms |
10588 KB |
Output is correct |
14 |
Correct |
8 ms |
9816 KB |
Output is correct |
15 |
Correct |
7 ms |
10332 KB |
Output is correct |
16 |
Correct |
7 ms |
9820 KB |
Output is correct |
17 |
Correct |
7 ms |
10076 KB |
Output is correct |
18 |
Correct |
7 ms |
10264 KB |
Output is correct |
19 |
Correct |
7 ms |
10076 KB |
Output is correct |
20 |
Correct |
7 ms |
11356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
428 KB |
Output is correct |
10 |
Correct |
0 ms |
508 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
5 ms |
8284 KB |
Output is correct |
20 |
Correct |
6 ms |
8128 KB |
Output is correct |
21 |
Correct |
5 ms |
8128 KB |
Output is correct |
22 |
Correct |
7 ms |
8288 KB |
Output is correct |
23 |
Correct |
5 ms |
8284 KB |
Output is correct |
24 |
Correct |
5 ms |
8284 KB |
Output is correct |
25 |
Correct |
5 ms |
8284 KB |
Output is correct |
26 |
Correct |
5 ms |
8240 KB |
Output is correct |
27 |
Correct |
6 ms |
8284 KB |
Output is correct |
28 |
Correct |
5 ms |
8340 KB |
Output is correct |
29 |
Correct |
7 ms |
8280 KB |
Output is correct |
30 |
Execution timed out |
2032 ms |
28984 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |