Submission #917360

#TimeUsernameProblemLanguageResultExecution timeMemory
917360CookieGroup Photo (JOI21_ho_t3)C++14
44 / 100
5008 ms468 KiB
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359, prec = 1e-9;;
using u128 = __uint128_t;
//const int x[4] = {1, 0, -1, 0};
//const int y[4] = {0, -1, 0, 1};
const ll mod = 1e9 + 7;
const int mxn = 1e6 + 69, mxq = 1e5 + 5, sq = 500, mxv = 1e6 + 5;
//const int base = (1 <<18);
const ll inf = 1e9 + 5, neg = -69420;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
//const int x[9] = {0, 1, 1, -1, -1, 2, -2, 2, -2};
//const inty[9] = {0, 2, -2, 2, -2, 1, 1, -1, -1};

int n;
int dp[5005], a[5005], toa[5005];
void solve(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i]; toa[a[i]] = i;
    }
    for(int i = 1; i <= n; i++){
        dp[i] = inf;

        for(int j = i; j >= 1; j--){
            int cost = 0;
            for(int k = j; k <= i; k++){
                for(int l = 1; l <= j - 1; l++){
                    cost += (toa[l] > toa[k]);
                }
                for(int l = k; l <= i; l++){
                    cost += (toa[k] < toa[l]);
                }
            }
            dp[i] = min(dp[i], dp[j - 1] + cost);
        }
        //cout << dp[i] << " ";
    }
    cout << dp[n];
}
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen("COLLEGE.INP", "r", stdin);
    //freopen("COLLEGE.OUT", "w", stdout);

    int tt; tt = 1;
    while(tt--){
        solve();

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