#include "books.h"
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
using namespace std;
using ll = long long;
constexpr ll inf = 1e18;
template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; }
template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; }
template<class M>
class SparseTable {
using T = typename M::T;
int n;
vector<vector<T>> dat;
public:
SparseTable(vector<T> a) {
n = a.size();
int h = __lg(n + 1) + 1;
dat.assign(h + 1, vector<T>(n, M::id()));
rep (i, n) dat[0][i] = a[i];
rep (i, h) rep (j, n - (1 << i)) {
dat[i + 1][j] = M::op(dat[i][j], dat[i][j + (1 << i)]);
}
}
T prod(int l, int r) {
assert(0 <= l && l <= r && r <= n);
int b = __lg(r - l);
return M::op(dat[b][l], dat[b][r - (1 << b)]);
}
};
struct Min {
using T = ll;
static T op(T a, T b) { return min(a, b); }
static T id() { return inf; }
};
struct Max {
using T = ll;
static T op(T a, T b) { return max(a, b); }
static T id() { return -inf; }
};
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size();
if (is_sorted(all(p))) return 0;
ll ans = 0;
{
while (p.back() == n - 1) {
if (s == n - 1) {
--s;
ans += 2;
}
p.pop_back();
--n;
}
reverse(all(p));
rep (i, n) p[i] = n - 1 - p[i];
s = n - 1 - s;
while (p.back() == n - 1) {
if (s == n - 1) {
--s;
ans += 2;
}
p.pop_back();
--n;
}
reverse(all(p));
rep (i, n) p[i] = n - 1 - p[i];
s = n - 1 - s;
}
rep (i, n) ans += abs(p[i] - i);
vector<ll> mns(n), mxs(n);
{
vector<bool> seen(n);
rep (i, n) {
if (seen[i]) continue;
int j = i;
int mx = 0;
do {
seen[j] = true;
chmax(mx, j);
j = p[j];
} while (i != j);
j = i;
do {
mns[j] = i;
mxs[j] = mx;
j = p[j];
} while (i != j);
}
}
SparseTable<Min> st1(mns);
SparseTable<Max> st2(mxs);
vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, inf));
dp[s][s] = 0;
rep (len, n) {
rep (l, n + 1 - len) {
int r = l + len;
if (dp[l][r] == inf) continue;
{
int l2 = st1.prod(l, r + 1);
int r2 = st2.prod(l, r + 1);
chmin(dp[l2][r2], dp[l][r]);
}
if (l != 0) chmin(dp[mns[l - 1]][max<int>(r, mxs[l - 1])], dp[l][r] + 1);
if (r != n - 1) chmin(dp[min<int>(l, mns[r + 1])][mxs[r + 1]], dp[l][r] + 1);
}
}
return ans + dp[0][n - 1] * 2;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
440 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
440 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
5 ms |
8284 KB |
Output is correct |
20 |
Correct |
4 ms |
8408 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
4 ms |
8284 KB |
Output is correct |
23 |
Correct |
4 ms |
8280 KB |
Output is correct |
24 |
Correct |
4 ms |
8444 KB |
Output is correct |
25 |
Correct |
5 ms |
8284 KB |
Output is correct |
26 |
Correct |
5 ms |
8284 KB |
Output is correct |
27 |
Correct |
4 ms |
8284 KB |
Output is correct |
28 |
Correct |
6 ms |
8284 KB |
Output is correct |
29 |
Correct |
4 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
440 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
5 ms |
8284 KB |
Output is correct |
20 |
Correct |
4 ms |
8408 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
4 ms |
8284 KB |
Output is correct |
23 |
Correct |
4 ms |
8280 KB |
Output is correct |
24 |
Correct |
4 ms |
8444 KB |
Output is correct |
25 |
Correct |
5 ms |
8284 KB |
Output is correct |
26 |
Correct |
5 ms |
8284 KB |
Output is correct |
27 |
Correct |
4 ms |
8284 KB |
Output is correct |
28 |
Correct |
6 ms |
8284 KB |
Output is correct |
29 |
Correct |
4 ms |
8284 KB |
Output is correct |
30 |
Runtime error |
583 ms |
1048576 KB |
Execution killed with signal 9 |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
8280 KB |
Output is correct |
2 |
Correct |
4 ms |
8516 KB |
Output is correct |
3 |
Correct |
8 ms |
8280 KB |
Output is correct |
4 |
Correct |
8 ms |
8280 KB |
Output is correct |
5 |
Correct |
7 ms |
8284 KB |
Output is correct |
6 |
Correct |
4 ms |
8284 KB |
Output is correct |
7 |
Correct |
4 ms |
8284 KB |
Output is correct |
8 |
Correct |
4 ms |
8284 KB |
Output is correct |
9 |
Correct |
6 ms |
8284 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
8284 KB |
Output is correct |
12 |
Correct |
4 ms |
8396 KB |
Output is correct |
13 |
Correct |
5 ms |
8284 KB |
Output is correct |
14 |
Correct |
6 ms |
8284 KB |
Output is correct |
15 |
Correct |
4 ms |
8284 KB |
Output is correct |
16 |
Correct |
4 ms |
8284 KB |
Output is correct |
17 |
Correct |
4 ms |
8284 KB |
Output is correct |
18 |
Correct |
4 ms |
8284 KB |
Output is correct |
19 |
Correct |
4 ms |
8520 KB |
Output is correct |
20 |
Correct |
5 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
440 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
5 ms |
8284 KB |
Output is correct |
20 |
Correct |
4 ms |
8408 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
4 ms |
8284 KB |
Output is correct |
23 |
Correct |
4 ms |
8280 KB |
Output is correct |
24 |
Correct |
4 ms |
8444 KB |
Output is correct |
25 |
Correct |
5 ms |
8284 KB |
Output is correct |
26 |
Correct |
5 ms |
8284 KB |
Output is correct |
27 |
Correct |
4 ms |
8284 KB |
Output is correct |
28 |
Correct |
6 ms |
8284 KB |
Output is correct |
29 |
Correct |
4 ms |
8284 KB |
Output is correct |
30 |
Runtime error |
583 ms |
1048576 KB |
Execution killed with signal 9 |
31 |
Halted |
0 ms |
0 KB |
- |