제출 #1332797

#제출 시각아이디문제언어결과실행 시간메모리
1332797LuvidiGroup Photo (JOI21_ho_t3)C++20
100 / 100
401 ms688 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fs first
#define sc second
#define pb push_back

struct bit{
    int n;
    vector<int> a;
    vector<pii> vv;
    bit(int t){
        n=t;
        a.resize(n+1);
    }
    void upd(int x,int y,bool b=1){
        if(b)vv.pb({x,y});
        while(x<=n){
            a[x]+=y;
            x+=x&-x;
        }
    }
    int qry(int x){
        int s=0;
        while(x){
            s+=a[x];
            x-=x&-x;
        }
        return s;
    }
    void clear(){
        for(auto[x,y]:vv){
            upd(x,-y,0);
        }
        vv.clear();
    }
};

void solve() {
    int n;
    cin>>n;
    int a[n+1];
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        a[x]=i;
    }
    bit b1(n),b2(n);
    int dp[n+1];
    dp[0]=0;
    for(int i=1;i<=n;i++){
        dp[i]=1e9;
        b1.upd(a[i],1);
        b2.clear();
        int s=0;
        for(int j=i;j;j--){
            s+=i-b1.qry(a[j]);
            s-=b2.qry(a[j]-1);
            b2.upd(a[j],1);
            dp[i]=min(dp[i],s+dp[j-1]);
        }
    }
    cout<<dp[n]<<'\n';
}

int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    solve();
}
#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...