제출 #1329557

#제출 시각아이디문제언어결과실행 시간메모리
1329557SinaPourkashaniGroup Photo (JOI21_ho_t3)C++20
100 / 100
182 ms234632 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector <pair<ll, ll>> vp;
typedef pair<ll, ll> pll;
typedef map <ll, ll> mll;
typedef set <ll> sll;
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define print(x) for (auto i : x) cout << i << ' '; cout << '\n';
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL);

const ll maxn = 5e3+5;
const ll mod = 1e9+7;
const ll inf = 2e18;

ll pw(ll a, ll b, ll M = mod) {ll ans = 1; for (; b; a = ((a * a) % M), b >>= 1) if (b & 1) ans = (ans * a) % M; return ans;}

ll n, a[maxn], ind[maxn], inv[maxn], cnt[maxn][maxn], dp[maxn], cnt2[maxn][maxn], sm[maxn];

int main() {
    FastIO

    cin >> n;

    for (ll i = 1; i <= n; i++) {
        cin >> a[i];
        ind[a[i]] = i;
        for (ll j = 1; j < i; j++) {
            if (a[j] < a[i]) {
                inv[a[i]]++;
            }
        }

        sm[a[i]] = a[i]-1-inv[a[i]];
    }

    for (ll i = 1; i <= n; i++) {
        cnt2[i][i] = ind[i]-1-inv[i];
        for (ll j = i+1; j <= n; j++) {
            cnt2[i][j] = cnt2[i][j-1] + ind[j]-1-inv[j] - sm[j];
        }

        for (ll k = 1; k < ind[i]; k++) {
            if (a[k] > i) sm[a[k]]--;
        }
    }

    for (ll i = 1; i <= n; i++) {
        for (ll j = i+1; j <= n; j++) {
            cnt[i][j] = cnt[i][j-1] + inv[j];
        }

        for (ll k = ind[i]+1; k <= n; k++) {
            if (a[k] > i) inv[a[k]]--;
        }
    }

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

    cout << dp[1] << '\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...