Submission #1329584

#TimeUsernameProblemLanguageResultExecution timeMemory
1329584namiousGroup Photo (JOI21_ho_t3)C++20
0 / 100
0 ms344 KiB
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long
#pragma GCC optimize("O3,unroll-loops")
#define fast_io ios_base::sync_with_stdio(0) ,cin.tie(0),cout.tie(0);

const int maxn = 5e3+3 , inf = 1e18+18;

int n,a[maxn],p[maxn],dp[maxn],inv[maxn][maxn],cnt[maxn][maxn];

int32_t main(){
    fast_io

    cin >> n; for(int i = 1 ; i <= n ; i++) cin >> a[i] , p[a[i]] = i;

    for(int i = 1 ; i <= n ; i++) for(int j = i+1 ; j <= n ; j++){
        inv[j][j-i+1] = inv[j-1][j-i] + (p[j] > p[i]);
        // cout << j << " " << j-i+1 << " --- " << inv[j][j-i+1] << endl;
    }

    for(int i = n ; i >= 1 ; i--){
        for(int j = 1 ; j <= i ; j++){
            cnt[i][j] = cnt[i][j-1] + inv[i][j] + (i-j+1-p[i-j+1]);
            // cout << i << " " << j << " *** " << cnt[i][j] << endl;
        }
        for(int j = p[i]+1 ; j <= n ; j++) p[a[j]]--;
    }

    for(int i = 1 ; i <= n ; i++) dp[i] = inf;
    for(int i = 1 ; i <= n ; i++) for(int j = 1 ; j <= i ; j++){
        dp[i] = min(dp[i] , cnt[i][j]+dp[i-j]);
    }

    cout << dp[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...