Submission #476885

#TimeUsernameProblemLanguageResultExecution timeMemory
476885RainbowbunnyGroup Photo (JOI21_ho_t3)C++17
100 / 100
262 ms67364 KiB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 5005;
const int INF = 1e9;

int n;
int H[MAXN], dp[MAXN], pos[MAXN];
int f[MAXN][MAXN];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        dp[i] = INF;
        cin >> H[i];
        pos[H[i]] = i;
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j < i; j++)
        {
            f[i][j] = f[i][j - 1] + (pos[i] < pos[j]);
        }
    }
    for(int i = 0; i <= n; i++)
    {
        int Cost = 0, Inv = 0;
        for(int j = i + 1; j <= n; j++)
        {
            Cost += f[j][i];
            Inv += (f[j][j - 1] - f[j][i]);
            dp[j] = min(dp[j], dp[i] + Cost + min(Inv, (j - i) * (j - i - 1) / 2 - Inv));
        }
    }
    cout << dp[n] << '\n';
}
#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...