代码
作者:
代码人生
,
2024-08-20 11:16:20
,
所有人可见
,
阅读 9
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 5e3 + 10;
int n,m;
int a[N];
int f[N][N];
int main(){
scanf("%d",&n);
m = ceil(1.0 * (n + 1) / 2);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
memset(f,0x3f,sizeof(f));
for(int i=1;i<=n;i++) f[i][0] = 0;
for(int i=2;i<=n;i++){
for(int j=1;j<=m&&j<=i;j++){
f[i][j] = f[i - 1][j];
int x = a[i - 1] - (a[i] - 1),y = a[i + 1] - (a[i] - 1);
if(a[i - 1] < a[i]) x = 0;
if(a[i + 1] < a[i]) y = 0;
if(i < n){
f[i][j] = min(f[i][j],f[i - 1][j - 1] + x + y);
}
}
}
for(int i=1;i<=m;i++){
if(f[n][i] == 0x3f3f3f3f) f[n][i] = -1;
printf("%d ",f[n][i]);
}
return 0;
}