제출 #1329604

#제출 시각아이디문제언어결과실행 시간메모리
1329604nariman_mt87Group Photo (JOI21_ho_t3)C++20
100 / 100
498 ms392672 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long int
#define lli long long int
#define pii pair<ll,ll>
#define all(x) x.begin(),x.end()
#define mk make_pair
#define pb push_back
#define se  second
#define fi first
#define nn '\n'
#define lc (id << 1)
#define rc ((id << 1) ^ 1)
#define mid (l + r) / 2
#define txt freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define T int T;cin>>T;while(T--)
#define ashar(x) fixed<<setprecision(x)
#define migmig ios_base::sync_with_stdio(0);cin.tie(0);
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const lli maxn = 5000 + 10, mod = 998244353, sq = 400, lg = 60, inf = 1e9;
ll pw(ll a, ll b) { return (!b ? 1 : (b & 1 ? a * pw(a * a % mod, b / 2) % mod : pw(a * a % mod, b / 2) % mod)); }//a*pw(b,mod-2) == (a/b)%mod
ll n, m, k;
ll a[maxn], cnt1[maxn][maxn], cnt2[maxn][maxn], dp[maxn];
int main(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for(int i = 1; i <= n; i++){
        for(int j = i + 1; j <= n; j++){
            if(a[j] > a[i]) cnt1[a[i]][a[j]] = 1;
            else cnt2[a[i]][a[j]] = 1;
        }
        for(int j = 1; j <= n; j++){
            cnt1[a[i]][j] = cnt1[a[i]][j - 1] + cnt1[a[i]][j];
            cnt2[a[i]][j] = cnt2[a[i]][j - 1] + cnt2[a[i]][j];
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            //cerr << i << " " << j << " " << cnt1[i][j] << " " << cnt2[i][j] << nn;
            cnt1[i][j] = cnt1[i][j] + cnt1[i - 1][j];
            cnt2[i][j] = cnt2[i][j] + cnt2[i - 1][j];
        }
    }
    for(int i = 1; i <= n; i++){
        dp[i] = inf;
        for(int j = i; j >= 1; j--){
            dp[i] = min(dp[i], dp[j - 1] + (cnt1[i][i] - cnt1[j - 1][i]) + (cnt2[i][j - 1] - cnt2[j - 1][j - 1]));
            //cerr << i << " " << j << " " << (cnt1[i][i] - cnt1[j - 1][i]) << " " << (cnt2[i][j - 1] + cnt2[j - 1][j - 1]) << nn;
        }
        //cerr << dp[i] << nn;
    }
    cout << dp[n] << nn;
}
#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...