제출 #1269387

#제출 시각아이디문제언어결과실행 시간메모리
1269387gayGroup Photo (JOI21_ho_t3)C++20
100 / 100
496 ms196524 KiB
#include <bits/stdc++.h>
#include <experimental/random>
#include <random>

using namespace std;
using ll = long long;
using ld = long double;

const ll INF = 1e18, MOD = 998244353;

void solve();

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll q = 1;
    //cin >> q;
    while (q--) {
        solve();
    }
}

struct fenwick {
    vector<ll> a;
    void change(ll i, ll x) {
        for (; i < a.size(); i += i & -i) {
            a[i] += x;
        }
    }
    ll get(ll l, ll r) {
        ll ans = 0;
        for (; l > 0; l -= l & -l) {
            ans -= a[l];
        }
        for (; r > 0; r -= r & -r) {
            ans += a[r];
        }
        return ans;
    }
};

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

    vector<vector<ll>> val(n, vector<ll>(n));
    fenwick t1; t1.a.resize(n + 1);
    vector<ll> cur(n);
    for (int i = 0; i < n; i++) {
        cur[i] = t1.get(pos[i] + 1, n);
        t1.change(pos[i] + 1, 1);
        ll cnt = 0;
        fenwick t2; t2.a.resize(n + 1);
        for (int j = i; j >= 0; j--) {
            cnt -= t2.get(0, pos[j]);
            cnt += t1.get(pos[j] + 1, n);
            val[j][i] = cnt;
            t2.change(pos[j] + 1, 1);
        }
    }

    vector<ll> dp(n, INF);
    dp[0] = 0;
    for (int i = 1; i < n; i++) {
        for (int j = i; j > 0; j--) {
            dp[i] = min(dp[i], dp[j - 1] + val[j][i]);
        }
        dp[i] = min(dp[i], val[0][i]);
    }

    cout << dp[n - 1];
}
#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...