이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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];
lli costo(lli l, lli r){
if (l != r){
return c[l][r] = c[l][r - 1] + mov[l][r] - menoresderecha(r, pos[r]) + menoresderecha(l, pos[r]);
}
c[l][r] = mayores[l][pos[l]];
return c[l][r];
}
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, 0){
dp[i] = min(dp[i], dp[j] + costo(j + 1, i));
}
}
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... |