This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 5e3 + 2;
const int inf = 2e9 + 2;
int dp[N][N], pref[N][N], p[N], ip[N], mn[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n; cin >> n;
for (int i = 1; i <= n; i++) {
cin >> p[i];
ip[p[i]] = i;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
if (ip[j] > ip[i]) pref[i][j] = 1;
pref[i][j] += pref[i][j - 1];
}
}
for (int i = 1; i <= n; i++) {
mn[i] = inf;
for (int j = 1; j <= i; j++) dp[i][j] = inf;
}
for (int i = 1; i <= n; i++) {
dp[i][1] = mn[i] = mn[i - 1] + pref[i][i - 1];
for (int j = 2; j <= i; j++) {
dp[i][j] = dp[i - 1][j - 1] + 2 * pref[i][i - j] - pref[i][i - 1] + j - 1;
smin(mn[i], dp[i][j]);
}
}
cout << mn[n] << en;
return 0;
}
# | 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... |