이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 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... |