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

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:

11

|2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9

And for the second sample:

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[N],m[N];
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[N][N],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[n][i]);
    cout<<ans;
    return 0;
}

 

 

 

转载于:https://www.cnblogs.com/candy99/p/5875191.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值