제출 #413388

#제출 시각아이디문제언어결과실행 시간메모리
413388ocarimaGroup Photo (JOI21_ho_t3)C++14
100 / 100
1953 ms626092 KiB
#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];

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) 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 - 1] + c[j][i]);
        }
    }

    cout << dp[n];

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...