Submission #1182689

#TimeUsernameProblemLanguageResultExecution timeMemory
1182689anteknneGroup Photo (JOI21_ho_t3)C++20
100 / 100
227 ms98664 KiB
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;

#define pb push_back
#define pii pair<ll, ll>
#define pll pair<ll, ll>
#define st first
#define nd second
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define debug false

const int MAXN = 5017;
int a[MAXN + 1];
int dp[MAXN + 1];
int pos[MAXN + 1];
int pom[MAXN][MAXN];

int main () {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n;
    cin >> n;

    for (int i = 0; i < n; i ++) {
        cin >> a[i];
        a[i] --;
        pos[a[i]] = i;
    }

    for (int i = 1; i <= n; i ++) {
        dp[i] = INT_MAX;
    }
    dp[0] = 0;

    for (int i = n - 2; i >= 0; i --) {
        for (int j = 0; j < n; j ++) {
            pom[i][j] = pom[i + 1][j] + (j <= a[i + 1]);
        }
	}

    for (int i = 1; i <= n; i ++) {
        int akt = 0;
        for (int j = i - 1; j >= 0; j --) {
			akt += pom[pos[j]][j] - 2 * pom[pos[j]][i] + (n - i);
            dp[i] = min(dp[i], dp[j] + akt);
		}
        //cout << dp[i] << "\n";
    }

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