이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5000;
int utan[maxn][maxn], ert[maxn][maxn];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
vector<int> v(n), inv(n);
for(int i = 0; i < n; i++){
cin>>v[i];
--v[i];
inv[v[i]] = i;
}
for(int i = 0; i < n; i++)
for(int j = i-1; j >= 0; j--)
utan[i][j] = utan[i][j+1] + (inv[i] < inv[j] ? 1 : 0);
vector<int> db(n);
iota(db.begin(), db.end(), 0);
for(int i = 0; i < n; i++){
ert[i][i] = db[inv[i]];
for(int j = i+1; j < n; j++)
ert[i][j] = ert[i][j-1] + db[inv[j]] - utan[j][i];
for(int j = inv[i]+1; j < n; j++)
db[j]--;
}
/*cout<<"ert:\n";
for(int i = 0; i < n; i++){
for(int j = i; j < n; j++)
cout<<ert[i][j]<<' ';
cout<<'\n';
}*/
vector<int> dp(n+1, (int)1e9);
dp[0] = 0;
for(int i = 1; i <= n; i++)
for(int j = i; j > 0; j--)
dp[i] = min(dp[i], dp[j-1] + ert[j-1][i-1]);
cout<<dp[n]<<'\n';
return 0;
}
/*
5
3 5 2 4 1
*/
# | 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... |