Submission #1338358

#TimeUsernameProblemLanguageResultExecution timeMemory
1338358hyyhGroup Photo (JOI21_ho_t3)C++20
64 / 100
5093 ms664 KiB
#include <iostream>
#include <math.h>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <iomanip>
#include <set>
#include <bitset>
#include <bit>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using piii = tuple<int,int,int>;
#define endl '\n'
#define f first
#define s second

int const N = 5e3+10;

int INF = 1e9+10;

int dp[N];

int vc[N];

int fenwick[8*N];

int si;

void update(int n, int val){
    for(;n <= si;n += n&-n) fenwick[n] += val;
}

int sum(int n){
    int ans = 0;
    for(;n > 0;n -= n&-n) ans += fenwick[n];
    return ans;
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;cin >> n;
    si = pow(2,ceil(log2(n)));
    for(int i{};i < n;i++){
        cin >> vc[i];
    }
    dp[2] = 0;
    for(int t{3};t <= n;t++){
        dp[t] = INF;
        vector<pii> pos(t+1);
        int i = 0;
        for(int j{};j < n;j++){
            if(vc[j] <= t){
                pos[vc[j]] = {j,i};
                i++;
            }
        }
        for(int i{t};i >= 1;i--){
            int cnt = 0;
            memset(fenwick,0,sizeof fenwick);
            for(int j{i};j <= t;j++){
                int val = t-pos[j].s;
                cnt += val-1;
                cnt -= sum(val);
                update(val,1);
            }
            dp[t] = min(dp[t],dp[i-1]+cnt);
        }
    }
    cout << dp[n];
}
#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...