Submission #493794

#TimeUsernameProblemLanguageResultExecution timeMemory
493794nathanlee726Group Photo (JOI21_ho_t3)C++14
100 / 100
420 ms392496 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long #define int ll #define pii pair<int,int> #define X first #define Y second #define F(n) Fi(i,n) #define Fi(i,n) Fl(i,0,n) #define Fl(i,l,n) for(int i=l;i<n;i++) #define all(x) x.begin(),x.end() #define sz(x) ((int)x.size()) #define abs(x) ((x)>0?(x):-(x)) #define pb push_back int n,tab[5005][5005],stab[5005][5005],a[5005],b[5005],dp[5005]; int qry(int l1,int r1,int l2,int r2){ //cout<<l1<<" "<<r1<<" "<<l2<<" "<<r2<<endl; //cout<<stab[r1][r2]<<" "<<stab[r1][l2-1]<<" "<<stab[l1-1][r2]<<" "<<stab[l1-1][l2-1]<<endl; return stab[r1][r2]-stab[r1][l2-1]-stab[l1-1][r2]+stab[l1-1][l2-1]; } signed main(){ ios_base::sync_with_stdio(0);cin.tie(0); cin>>n; F(n)cin>>a[i],b[a[i]]=i; int s=0; memset(tab,0,sizeof(tab)); F(n)for(int j=i+1;j<n;j++){ if(b[i+1]<b[j+1])s++,tab[i+1][j+1]=0; else tab[i+1][j+1]=1; } for(int i=0;i<=n;i++)stab[i][0]=stab[0][i]=0; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++)stab[i][j]=tab[i][j]+stab[i-1][j]+stab[i][j-1]-stab[i-1][j-1]; } /*F(n+1){ Fi(j,n+1)cout<<stab[i][j]<<" "; cout<<endl; }*/ dp[n]=s; for(int i=n-1;i>=1;i--){ dp[i]=1e9; for(int j=i+1;j<=n;j++){ int tt=qry(1,i,i+1,j); dp[i]=min(dp[i],dp[j]-(i*(j-i))+tt*2); } } /*int no=n; for(int i=n-1;i>=1;i--){ int tt=qry(1,i,i+1,no); cout<<tt<<" "<<(i*(no-i))-tt<<endl; if(tt<(i*(no-i))-tt){ s-=((i*(no-i))-tt*2); no=i; } cout<<s<<endl; }*/ s=1e9; F(n)s=min(dp[i+1],s); cout<<s<<endl; return 0; } /* 5 4 5 3 5 1 4 3 4 1 3 1 2 1 8 7 */
#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...