您的位置:首页 > 其它

Codeforces Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]

2016-09-15 18:28 423 查看
E. Sonya and Problem Wihtout a Legend

time limit per test
5 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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

题意:一个序列加减1变成严格单增最少操作数

很像LIS,然后就没时间了

d[i][j]表示前i个以j结尾的最少操作数

d[i][j]=min{d[i-1][k]+a[i]-j:k<=?j}

j离散化 --> m[j] sort(m) 可以发现最后的一个一定可以是原序列中的值

严格单调递增,直接处理好难我不会(因为那样的话不一定原序列结尾了,可能+1),网上题解上都是让a[i]-=i变成不严格

可以用mn维护min(d[i-1][k]),少了一层循环

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=3005;
const ll INF=1e19;
int n,k,a
,m
;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
ll d

,ans=INF;
void dp(){
for(int i=1;i<=n;i++){
ll mn=INF;
for(int j=1;j<=k;j++){
mn=min(mn,d[i-1][j]);
d[i][j]=mn+abs(a[i]-m[j]);
}
}
}

int main(int argc, const char * argv[]) {
n=read();
for(int i=1;i<=n;i++) a[i]=m[i]=read()-i;
sort(m+1,m+1+n);
k=unique(m+1,m+1+n)-m-1;//printf("k %d\n",k);
dp();
for(int i=1;i<=k;i++) ans=min(ans,d
[i]);
cout<<ans;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: