Submission #1116051

#TimeUsernameProblemLanguageResultExecution timeMemory
1116051LucaIlieGroup Photo (JOI21_ho_t3)C++17
64 / 100
5056 ms100700 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], cost[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++ ) {
            for ( int i = 1; i <= n; i++ ) {
                if ( l <= h[i] && h[i] <= r )
                    cost[l][r] += lower[i][h[i] - 1] - lower[i][l - 1] + lower[i][n] - lower[i][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] + cost[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...