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>
#define speed ios_base::sync_with_stdio(0); cin.tie(0)
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MAX=5e3+10,P=998244353;
const ll INF=0x3f3f3f3f,oo=0x3f3f3f3f3f3f3f3f;
int n;
int a[MAX];
ll dp[MAX];
int BIT[MAX];
void update(int x,int val) {
while (x<MAX) {
BIT[x]+=val;
x+=(-x&x);
}
}
int query(int x) {
int ans=0;
while (x) {
ans+=BIT[x];
x-=(-x&x);
}
return ans;
}
int main() {
speed;
cin>>n;
for (int i=1;i<=n;i++) {
cin>>a[i];
}
for (int i=1;i<=n;i++) {
vector<int> arr;
arr.push_back(0);
for (int j=1;j<=n;j++) {
if (a[j]>=n-i+1) arr.push_back(a[j]-(n-i));
}
vector<int> pos(i+1);
for (int j=1;j<=i;j++) pos[arr[j]]=j;
memset(BIT,0,sizeof(BIT));
ll now=0;
ll ans=oo;
for (int j=1;j<=i;j++) {
now-=j-1;
now+=query(pos[j]);
update(pos[j],1);
now+=pos[j]-1;
ans=min(ans,now+dp[i-j]);
}
dp[i]=ans;
// cout<<dp[i]<<" ";
}
cout<<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... |