Submission #389638

#TimeUsernameProblemLanguageResultExecution timeMemory
389638mariowongGroup Photo (JOI21_ho_t3)C++14
100 / 100
313 ms67572 KiB
#include <bits/stdc++.h> using namespace std; int dp[5005],a[5005],pos[5005],n,ans,ps[5005][5005]; int main(){ ios::sync_with_stdio(false); cin >> n; for (int i=1;i<=n;i++){ cin >> a[i]; pos[a[i]]=i; } for (int i=1;i<=n;i++){ for (int j=i+1;j<=n;j++){ if (pos[i] > pos[j]) ans++; } } for (int i=2;i<=n;i++){ for (int j=1;j+i-1<=n;j++){ ps[i][j]=ps[i-1][j]+ps[i-1][j+1]-ps[i-2][j+1]; if (pos[j] > pos[j+i-1]) ps[i][j]++; } } for (int i=1;i<=n;i++){ for (int j=1;j<=i;j++){ dp[i]=min(dp[i],dp[j-1]-(ps[i-j+1][j]-(i-j+1)*(i-j)/2+ps[i-j+1][j])); } } cout << ans+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...