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>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define lli long long int
#define vi vector<int>
#define vlli vector<long long int>
#define pii pair<int, int>
#define plli pair<lli, lli>
#define rep(i, a, b) for(lli i = (a); i <= (b); i++)
#define repa(i, a, b) for(lli i = (a); i >= (b); i--)
#define repv(x, v) for(auto x : v)
#define debug(x) cout << #x << " = " << x << endl
#define debugsl(x) cout << #x << " = " << x << ", "
#define debugarr(x, a, b) cout << #x << " = ["; rep(ii, a, b) cout << x[ii] << ", "; cout << "]\n"
#define pb push_back
#define nl "\n"
#define MAX_N 5002
#define INF (1ll << 62)
#define menoresderecha(x, pos) ((x - 1) - menores[x][pos])
lli n, h[MAX_N], dp[MAX_N], pos[MAX_N], mov[MAX_N][MAX_N], c[MAX_N][MAX_N], menores[MAX_N][MAX_N], mayores[MAX_N][MAX_N], costo;
int main()
{
fastio;
cin >> n;
rep(i, 1, n){ cin >> h[i]; pos[h[i]] = i; }
rep(i, 1, n){
rep(j, 1, n){
mayores[j][i] = mayores[j][i - 1];
menores[j][i] = menores[j][i - 1];
}
rep(j, h[i] + 1, n) ++menores[j][i];
repa(j, h[i] - 1, 1) ++mayores[j][i];
}
rep(i, 1, n){
rep(j, i + 1, n){
mov[i][j] = menores[j][pos[j]] - menores[i][pos[j]] + mayores[j][pos[j]];
}
}
rep(i, 1, n){
dp[i] = INF;
repa(j, i, 1){
if (j == i) costo = c[j][i] = mayores[j][pos[j]];
else c[j][i] = c[j][i - 1] + mov[j][i] - menoresderecha(i, pos[i]) + menoresderecha(j, pos[i]);
dp[i] = min(dp[i], dp[j] + costo);
}
}
cout << dp[n];
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... |