This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
int n,a[5050],dp[5050],cnt[5050][5050],ind[5050];//cnt[i][j]=count of a[x] (x<i) s.t. a[x]>=j
ordered_set st;
int count(int l,int r){
return (cnt[r][n]-cnt[r][r])-(cnt[l-1][n]-cnt[l-1][r]);
}
int main(){
cin >> n;
for (int i=1;i<=n;i++) cin >> a[i],ind[a[i]]=i;
//for (int i=1;i<=n;i++) ind[i]+=ind[i-1];
for (int i=1;i<=n;i++){
for (int j=1;j<i;j++){
cnt[a[i]][a[j]]++,cnt[a[i]][n+1]--;
}
for (int j=1;j<=n+1;j++) cnt[a[i]][j]+=cnt[a[i]][j-1];
}
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++) cnt[i][j]+=cnt[i-1][j];
}
dp[0]=0;
ind[0]=INT_MAX;
for (int i=1;i<=n;i++){
dp[i]=INT_MAX;
int cnt=0;
st.clear();
st.insert(ind[i]);
for (int j=i-1;j>=0;j--){
dp[i]=min(dp[i],dp[j]+count(j+1,i)+cnt);
cnt+=st.size()-st.order_of_key(ind[j]);
st.insert(ind[j]);
}
}
cout << dp[n] << "\n";
}
# | 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... |