Submission #1116057

#TimeUsernameProblemLanguageResultExecution timeMemory
1116057LucaIlieGroup Photo (JOI21_ho_t3)C++17
100 / 100
351 ms242496 KiB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 5000;
int h[MAX_N + 1], p[MAX_N + 1], lower[MAX_N + 1][MAX_N + 1], costSmall[MAX_N + 1][MAX_N + 1], costBig[MAX_N + 1][MAX_N + 1], minCost[MAX_N + 1];

int main() {
    int n;

    cin >> n;
    for ( int i = 1; i <= n; i++ ) {
        cin >> h[i];
        p[h[i]] = i;
    }

    for ( int i = 1; i <= n; i++ ) {
        for ( int j = 1; j < i; j++ )
            lower[i][h[j]]++;
        for ( int j = 1; j <= n; j++ )
            lower[i][j] += lower[i][j - 1];
    }

    for ( int l = 1; l <= n; l++ ) {
        for ( int r = l; r <= n; r++ )
            costSmall[l][r] = costSmall[l][r - 1] + lower[p[r]][r - 1] - lower[p[r]][l - 1];
    }
    for ( int r = 1; r <= n; r++ ) {
        for ( int l = r; l >= 1; l--)
            costBig[l][r] = costBig[l + 1][r] + lower[p[l]][n] - lower[p[l]][r];
    }

    for ( int i = 1; i <= n; i++ ) {
        minCost[i] = n * n;
        for ( int j = 0; j < i; j++ )
            minCost[i] = min( minCost[i], minCost[j] + costSmall[j + 1][i] + costBig[j + 1][i] );
    }

    cout << minCost[n] << "\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...