This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// 還要更強
#include<iostream>
#include<queue>
#include<set>
#include<map>
#include<iomanip>
#include<math.h>
#include<cstring>
#include<stack>
#include<string.h>
#include<random>
#include<algorithm>
#include<vector>
#define ll long long
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a,b) for(int i=a;i>=b;i--)
#define S second
#define F first
#define INF (ll)(3e18)
#define pb push_back
#define AC ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int n,h[5005],pos[5005],pre[2][5005][5005],dp[5005];
bool ni[2][5005][5005];
signed main(){
AC;
cin>>n;
FOR(i,1,n+1){
cin>>h[i]; pos[h[i]]=i;
}
FOR(i,1,n+1)FOR(j,1,i){
if(pos[i]<pos[j]) ni[0][i][j]=1;
else ni[1][i][j]=1;
}
FOR(i,1,n+1)FOR(j,1,n+1){
pre[0][i][j]=pre[0][i-1][j]+pre[0][i][j-1]-pre[0][i-1][j-1]+ni[0][i][j];
pre[1][i][j]=pre[1][i-1][j]+pre[1][i][j-1]-pre[1][i-1][j-1]+ni[1][i][j];
//cout<<"pre["<<i<<"]["<<j<<"]="<<pre[i][j]<<endl;
}
dp[1]=0;
FOR(i,2,n+1){
dp[i]=pre[1][i][i];
FOR(j,1,i){
int tmp=pre[0][i][j]-pre[0][j][j],tmp2=pre[1][i][i]-pre[1][i][j]-pre[1][j][i]+pre[1][j][j];
//cout<<"i="<<i<<" j="<<j<<" tmp="<<tmp<<" tmp2="<<tmp2<<endl;
dp[i]=min(dp[i],tmp+tmp2+dp[j]);
}
//cout<<"dp["<<i<<"]="<<dp[i]<<endl;
}
cout<<dp[n]<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |