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

185 篇文章 0 订阅
116 篇文章 0 订阅

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[n][i]);
	cout<<ans<<endl;
	
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值