Submission #409475

#TimeUsernameProblemLanguageResultExecution timeMemory
409475534351Group Photo (JOI21_ho_t3)C++17
100 / 100
401 ms134472 KiB
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
    if (a > b) a = b;
}

template<class T, class U>
void ckmax(T &a, U b)
{
    if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)

const int MAXN = 5013;
const int INF = 1e9 + 7;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N;
int arr[MAXN], pos[MAXN];
int inv[MAXN][MAXN], inv1[MAXN][MAXN];
int dp[MAXN];

int32_t main()
{
    cout << fixed << setprecision(12);
    cerr << fixed << setprecision(4);
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> N;
    FOR(i, 1, N + 1)
    {
        cin >> arr[i];
        pos[arr[i]] = i;
    }
    FOR(i, 1, N + 1)
    {
        FOR(j, i + 1, N + 1)
        {
            inv[j][i] = inv[j - 1][i] + inv[j][i - 1] - inv[j - 1][i - 1] + (bool) (pos[j] < pos[i]);
        }
    }
    FOR(i, 1, N + 1)
    {
        FOR(j, 1, N + 1 - i)
        {
            //j...j+i.
            inv1[j][j + i] = inv1[j + 1][j + i] + inv1[j][j + i - 1] - inv1[j + 1][j + i - 1] + (bool) (pos[j + i] > pos[j]);
        }
    }
    dp[0] = 0;
    FOR(i, 1, N + 1)
    {
        dp[i] = INF;
        FOR(j, 0, i)
        {
            ckmin(dp[i], dp[j] + inv[i][j] + inv1[j + 1][i]);
        }
    }
    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...