Submission #1329528

#TimeUsernameProblemLanguageResultExecution timeMemory
1329528benyGroup Photo (JOI21_ho_t3)C++20
64 / 100
5103 ms232204 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;

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long

const int N = 5e3 + 5;
int dp[N];
int cnt[N][N], pd[N][N];
int a[N], b[N], mx[N][N];

int main(){

    int n; cin >> n;

    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) b[a[i]] = i;
    for (int i = 1; i <= n; i++) {
        mx[i][i] = b[i];
        for (int j = i + 1; j <= n; j++) {
            mx[i][j] = max(mx[i][j - 1], b[j]);
        }
    }

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

    for (int i = 1; i <= n; i++) {
        dp[i] = 1e9;

        for (int j = 0; j < i; j++) {
            int res = dp[j];
            for (int k = j + 1; k <= i; k++) {
                res += cnt[k][i + 1];
            }
            res += pd[j + 1][i];

            dp[i] = min(dp[i], res);
        }
    }

    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...