Submission #1271181

#TimeUsernameProblemLanguageResultExecution timeMemory
1271181witek_cppGroup Photo (JOI21_ho_t3)C++20
100 / 100
812 ms196596 KiB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
typedef long long ll;

#define st first
#define nd second
#define f(a, c, b) for (int a = c; b > a; a++)
#define pb push_back
#define all(a) a.begin(), a.end()
#define wczytaj(a, c, n) a.resize(n); f(i, c, n) cin >> a[i];
#define sz(a) int(a.size())
#define wypisz(a, c) f(i, c, sz(a)) cout << a[i] << " "; cout << "\n";

int main() {
    //ios_base::sync_with_stdio(0);
    //cin.tie(0);
    int n;
    cin >> n;
    vector<int> a(n + 1);
    f(i, 1, n + 1) cin >> a[i];
    vector<int> dp(n + 1, 1'000'000'000);
    vector<vector<int>> ileza(n + 1, vector<int>(n + 1, 0));
    vector<vector<int>> ile_przed(n + 1, vector<int>(n + 1, 0));
    f(i, 1, n + 1) {
        f(j, 1, i) ile_przed[i][a[j]]++;
        f(j, 1, n + 1) ile_przed[i][j] += ile_przed[i][j - 1];
    }
    f(i, 1, n + 1) {
        f(j, i + 1, n + 1) ileza[i][a[j]]++;
        f(j, 1, n + 1) ileza[i][j] += ileza[i][j - 1];
    }
    vector<int> q(n + 1);
    f(i, 1, n + 1) q[a[i]] = i;
    dp[0] = 0;
    f(i, 1, n + 1) { //rozwazamy dp ze skonczylo sie na i - 1 a teraz musimy jakos to dopasowac
        int aktl_dodanie = 0;
        for (int j = n; j >= i; j--) {
            aktl_dodanie += (q[j] + ileza[q[j]][i - 1] + ileza[q[j]][n] - ileza[q[j]][j]) - (i + (n - j));
        }
        dp[n] = min(dp[n], dp[i - 1] + aktl_dodanie);
        /*if (i == 1) {
            cout << "aktl_dodanie to " << aktl_dodanie << "\n";
        }*/
        for (int j = n; j > i; j--) { //odejmuje element o wartosci j
            aktl_dodanie += (j - i);
            aktl_dodanie -= ile_przed[q[j]][j - 1] - ile_przed[q[j]][i - 1];
            /*if (i == 1) {
                cout << "od siebie odejmuje " << (q[j] + ileza[q[j]][i - 1]) - i << "\n";
            }*/
            aktl_dodanie -= (q[j] + ileza[q[j]][i - 1]) - i;
            /*if (i == 1) {
                cout << "odejmuje val " << j << "  aktl_dodanie to teraz " << aktl_dodanie << "\n";
            }*/
            dp[j - 1] = min(dp[j - 1], aktl_dodanie + dp[i - 1]);
        }
    }
    cout << dp[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...