您的位置:首页 > 其它

Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend(贪心+DP)

2016-09-14 17:30 405 查看
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
题意:给一定长度最多为1000的数字序列a,让你求一个序列b,使得sgma|a[i]-b[i[|最小且b[i]严格递增。

分析:将a[i] - i后,只要再构造出一个非递减序列就可以了,这里有一个trick,最优解一定可以全部由原序列的数字构造出

证明过程:

    假设最优解序列存在一个bi不等于任何aj,且bi != bi-1 != bi+1,那么我们一定可以通过调整bi的值使其等于ai且答案变小,此时与假设矛盾,故此时不属于a的b内的元素一定是连续的一段一段的区间,对于同一个区间,在其大于bi的数一定等于小于bi的数(bi为中位数),于是我们又可以通过调整bi使其等于一个aj的值且对结果无影响,所以所有不属于a的元素都可以通过这种调整变成属于a的元素.

然后就可以n^2dp了,f[i][j]表示前i个数最后一个数为b[j]的最优解。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<ctime>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#define INF 21474836470000
#define eps 1e-9#define MAXN 3005
using namespace std;
int n,a[MAXN],b[MAXN];
long long f[MAXN][MAXN],pre[MAXN][MAXN];
int main()
{
scanf("%d",&n);
for(int i = 1;i <= n;i++)
{
scanf("%d",&a[i]);
a[i] -= i;
b[i] = a[i];
}
sort(b+1,b+1+n);
for(int i = 1;i <= n;i++)
{
pre[i][0] = INF;
for(int j = 1;j <= n;j++)
{

f[i][j] = pre[i-1][j] + abs(a[i] - b[j]);
pre[i][j] = min(pre[i][j-1],f[i][j]);
}
}
long long ans = INF;
for(int i = 1;i <= n;i++) ans = min(ans,f
[i]);
cout<<ans<<endl;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: