Submission #692761

#TimeUsernameProblemLanguageResultExecution timeMemory
692761owoovoGroup Photo (JOI21_ho_t3)C++14
100 / 100
409 ms165228 KiB
#include<bits/stdc++.h> using namespace std; int dp[5010],a[5010][5010]={},ori[5010],pre[5010][5010]={}; int q(int li,int ri,int dj,int uj){ return pre[ri][uj]-pre[li-1][uj]-pre[ri][dj-1]+pre[dj-1][li-1]; } int main(){ cin.tie(0); ios::sync_with_stdio(0); int n; cin>>n; for(int i=1;i<=n;i++){ cin>>ori[i]; dp[i]=1000000000; } for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ if(ori[i]<ori[j]){ a[ori[i]][ori[j]]=1; } } } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ pre[i][j]=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1]+a[i][j]; } } for(int i=1;i<=n;i++){ dp[i]=dp[i-1]+i-1-q(1,i,i,i); for(int j=0;j<i;j++){ dp[i]=min(dp[i],q(j+1,i,j+1,i)+(i-j)*j-q(1,j,j+1,i)+dp[j]); } } cout<<dp[n]<<"\n"; 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...