4.1-Q
Sonya was unable to think of a story for this problem, so here comes the formal description.
You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 3000) — the length of the array.
Next line contains n integer ai (1 ≤ ai ≤ 109).
Output
Print the minimum number of operation required to make the array strictly increasing.
Examples
Input
7
2 1 5 11 5 9 11
Output
9
Input
5
5 4 3 2 1
Output
12
Note
In the first sample, the array is going to look as follows:
2 3 5 6 7 9 11
|2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9
And for the second sample:
1 2 3 4 5
|5 - 1| + |4 - 2| + |3 - 3| + |2 - 4| + |1 - 5| = 12
将严格递增推导为非严格递增:
a[i] < a[i+1] —> a[i] <= a[i+1]-1 —> a[i]-i <= a[i+1]-(i+1)—> b[i]<=b[i+1]
dp[i][j]表示序列前i个数都单调递增且第i个数更改为不大于原序列中第j个数的最少代价,dp[i][j]=min (dp[i-1][k])+abs(a[i]-j),dp[i-1][k]取的是i-1中最小的,所以只要开个数组记录一下上一层的最小值就可以实现优化了
同志仍需努力,这些答案的脑回路跟我不一样
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<iomanip>
#define INF 0x3f3f3f3f
#define E 1e-12
#define N 1000001
#define LL long long
#define MOD 10000
using namespace std;
LL dp[3005][3005],n,a[3005],b[3005],d[3005];
int i,j;
int main()
{
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a[i];
a[i]-=i;
b[i]=a[i];
}
sort(b+1,b+1+n);
memset(d,INF,sizeof(d));
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(i==1)dp[i][j]=abs(a[i]-b[j]);
else dp[i][j]=d[j]+abs(a[i]-b[j]);
d[j]=min(d[j-1],dp[i][j]);
}
cout<<d[n]<<endl;
}