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>
using namespace std;
const int N = 5e3;
const int inf = 2e9;
int v[N],pos[N];
int dp[N];
int fen[N][3];
int n;
void add(int pos,int x,int id){
for(int i = pos;i < n;i|=(i + 1)){
fen[i][id]+=x;
}
}
int get(int pos,int id){
int r = 0;
for(int i = pos;i >= 0;i&=(i + 1),i--){
r+=fen[i][id];
}
return r;
}
int main(){
cin>>n;
for(int i = 0;i < n;i++){
cin>>v[i];
v[i]--;
pos[v[i]] = i;
dp[i] = inf;
}
int sum2 = 0;
for(int i = n - 1;i >= 0;i--){
sum2+=get(n - 1 - pos[i],0);
dp[i] = min(dp[i],sum2);
//cout<<sum2<<' ';
add(n - 1 - pos[i],1,0);
add(pos[i],1,2);
int sum = 0;
for(int j = i - 1;j >= 0;j--){
sum+=get(pos[j],2);
sum+=get(n - 1 - pos[j],1);
add(n - 1 - pos[j],1,1);
//cout<<j<<' '<<i<<' '<<sum<<'\n';
dp[j] = min(dp[j], dp[i] + sum);
}
for(int j = i - 1;j >= 0;j--){
add(n - 1 - pos[j],-1,1);
}
//cout<<dp[i]<<'\n';
}
cout<<dp[0];
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... |