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;
#define ll long long
#define ff first
#define ss second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 500'005;
const ll MOD = 1'000'000'007;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int a[5005][5005];
int bit[5005];
int h[N];
int loc[N];
void update(int x){
while(x < 5005){
++bit[x];
x += x & -x;
}
}
int get(int x){
int s = 0;
while(x){
s += bit[x];
x -= x & -x;
}
return s;
}
int dp[5005];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i = 1; i <= n; ++i){
cin>>h[i];
loc[h[i]] = i;
}
for(int i = 1; i <= n; ++i){
for(int j = 0; j < 5005; ++j)
bit[j] = 0;
for(int j = i; j <= n; ++j){
a[i][j] = a[i][j - 1] + j - i - get(loc[j]);
update(loc[j]);
}
}
for(int i = 0; i <= n; ++i){
dp[i] = a[1][n];
for(int j = 0; j < i; ++j)
dp[i] = min(dp[i], dp[j] - a[j + 1][i] + (i - j) * (i - j - 1) / 2 - a[j + 1][i]);
}
cout<<dp[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... |