Submission #469174

#TimeUsernameProblemLanguageResultExecution timeMemory
469174Killer2501Group Photo (JOI21_ho_t3)C++14
100 / 100
231 ms117624 KiB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const int N = 5e3 + 5;
const ll mod = 1e12 + 7;
ll n, m, a[N], b[N], x[N], dp[N], inv[N][N], tong;

void sol()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        b[a[i]] = i;
    }
    for (int i = 1; i <= n; i++)
    {
        tong = 0;
        for (int j = i - 1; j > 0; j--)
        {
            if (b[i] < b[j])
                ++tong;
            inv[i][j] = inv[i - 1][j] + tong;
        }
    }
    for (int i = 2; i <= n; i++)
    {
        dp[i] = mod;
        for (int j = i - 1; j >= 0; j--)
        {
            dp[i] = min(dp[i], dp[j] + inv[i][1] - inv[j][1] + (i - j) * (i - j - 1) / 2 - inv[i][j + 1] * 2);
        }
        //cout << dp[i] << " ";
    }
    cout << dp[n];
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    int test = 1;
    //cin >> test;
    while (test-- > 0)
        sol();
    return 0;
}
/*
5
2 1 3 5 4
*/
// ctrl + F8 = compile
// ctrl + F9 = run
#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...