Codeforces Round #371 (Div. 2) E dp



链接:戳这里


E. Sonya and Problem Wihtout a Legend
time limit per test5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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


题意:

长度为n的整数序列,要求加减数值ai使得所有的数严格上升,要求加减的值最小


思路:

1:使得一个序列严格上升,也就对应->每个数值ai减去i,求序列不下降

2:现在是加减最少的值使得序列不下降,这里的不下降意味着当前数ai要么不变,要么等于前一个或者后一个数

也就是说,新的序列bi满足不下降,里面的元素必须是ai中的元素

设置dp[i][j],表示当前以第i个数结尾,第i个数ai变成bi,bi对应原序列的一个数aj且使得加减的值最小

dp[i][j]=min(dp[i][j-1],dp[i-1][j]+abs(a[i]-b[j]));  a[i]表示原数组, b[j]表示a数组排好序的值

dp[i][j-1]:第i个数为第j-1小

dp[i-1][j]+abs(a[i]-b[j]):表示第i个数作为第j小


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
int n;
ll a[100100],b[100100];
ll dp[2][100100];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        cin>>a[i];
        a[i]-=i;
        b[i]=a[i];
    }
    sort(b+1,b+n+1);
    int now=0,last=1;
    for(int i=1;i<=n;i++){
        memset(dp[last],0,sizeof(dp[last]));
        swap(now,last);
        dp[now][1]=dp[last][1]+abs(a[i]-b[1]);
        for(int j=2;j<=n;j++){
            dp[now][j]=min(dp[now][j-1],dp[last][j]+abs(a[i]-b[j]));
        }
    }
    cout<<dp[now][n]<<endl;
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值