This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// 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;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |