Description

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  \le  n \le 3000)\) — the length of the array.

Next line contains \(n\) integer \(a_{i}(1 \le a_{i} \le  10^{9})\).

Output

Print the minimum number of operation required to make the array strictly increasing.

Sample Input

7

2 1 5 11 5 9 11

Sample Output

9

BZOJ1049 数字序列类似,所有我想到了\(O(N^{3})\)做法,果断TLE。标解懂了些,做法太神了,什么维护中位数,但就是不知道转移怎么会没有后效性。

此处介绍另一种做法。首先也是将单调上升变为单调不降(见BZOJ1049 数字序列)。\(f_{i,j}\)表示前\(i\)个数,最大为\(j\)的合法序列最小代价。转移方程

\[f_{i,j} = min\{f_{i-1,k} \}+\mid A_{i}-j \mid(k \le j)
\]

当然\(A\)值域太大,我们可以离散化。代码如下:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std; typedef long long ll;
#define inf (1LL<<50)
#define maxn (3010)
int N,cnt; ll f[maxn][maxn],ans = inf,A[maxn],B[maxn]; int main()
{
freopen("E.in","r",stdin);
freopen("E.out","w",stdout);
scanf("%d",&N);
for (int i = 1;i <= N;++i) scanf("%I64d",A+i),A[i] -= i;
memcpy(B,A,sizeof(B)); B[N+1] = -inf,B[N+2] = inf;
sort(B+1,B+N+3); cnt = unique(B+1,B+N+3)-B-1;
A[0] = -inf; A[N+1] = inf;
memset(f,0x7,sizeof(f)); f[0][1] = 0;
for (int i = 1;i <= N+1;++i)
{
ll tmp = inf;
for (int j = 1;j <= cnt;++j)
tmp = min(tmp,f[i-1][j]),f[i][j] = tmp+abs(A[i]-B[j]);
}
for (int i = 1;i <= cnt;++i) ans = min(ans,f[N+1][i]);
printf("%I64d",ans);
fclose(stdin); fclose(stdout);
return 0;
}

Codeforces 713 C Sonya and Problem Wihtout a Legend的更多相关文章

  1. 【CodeForces】713 C. Sonya and Problem Wihtout a Legend

    [题目]C. Sonya and Problem Wihtout a Legend [题意]给定n个数字,每次操作可以对一个数字±1,求最少操作次数使数列递增.n<=10^5. [算法]动态规划 ...

  2. codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)

    题目链接: C. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 ...

  3. codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)(将一个数组变成严格单增数组的最少步骤)

    E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...

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

    C. Sonya and Problem Wihtout a Legend 题目连接: http://codeforces.com/contest/713/problem/C Description ...

  5. 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 megaby ...

  6. Codeforces Round #371 (Div. 1) C - Sonya and Problem Wihtout a Legend

    C - Sonya and Problem Wihtout a Legend 思路:感觉没有做过这种套路题完全不会啊.. 把严格单调递增转换成非严格单调递增,所有可能出现的数字就变成了原数组出现过的数 ...

  7. Codeforces 713C Sonya and Problem Wihtout a Legend DP

    C. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...

  8. Codeforces 713C Sonya and Problem Wihtout a Legend(DP)

    题目链接   Sonya and Problem Wihtout a Legend 题意  给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$. 求把原序列变成 ...

  9. 把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend

    //把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend //dp[i][j]:把第i个数转成第j小的数,最小花费 //此题与po ...

随机推荐

  1. Qt OpenGL三维绘图

     简介 OpenGL是为三维绘图提供的标准应用编程接口. OpenGL处理的仅仅是三维绘图方面,而很少或是根本不提供图形用户界面编程方面的支持.OpenGL*应用程序的用户界面必须由其它工具包创建,比 ...

  2. GUI之CCControlExtension

    Introduction CCControl is inspired by the UIControl API class from the UIKit library of CocoaTouch. ...

  3. 仿QQ聊天软件2.0版

    地址:http://blog.csdn.net/u012027907/article/details/36952893 Oracle java

  4. javascript常用方法(慢慢整理)

    获取类型:[object object],[object function],[object Undefined]等 Object.prototype.toString.apply(obj); 获取对 ...

  5. 把QQ聊天记录插入数据库中

    最近在做毕设,其中一个环节是分析qq聊天记录,在分析之前需要先把qq聊天记录导出,然后存入数据库中,qq聊天记录导出后是文本文档,导出方式: 1.登录qq后,点击任意一个好友,查看与他的聊天记录,点击 ...

  6. Windows7服务无法启动的解决

    这周六,我接到了一个很诡异的案例,表现为任务栏右下角网络连接图标始终为一个红叉,已排除网卡硬件.链路和网卡驱动的问题.主板都新换了一块,可是问题依旧,这无疑将问题的根源指向了操作系统.本想通过网络疑难 ...

  7. 深入理解Javascript变量作用域

    在学习JavaScript的变量作用域之前,我们应当明确几点: a.JavaScript的变量作用域是基于其特有的作用域链的. b.JavaScript没有块级作用域. c.函数中声明的变量在整个函数 ...

  8. yieId浅谈

    例子:在不使用yieId时,通常我们都会采取先遍历再把元素加到新的List中 using (var reader = SqlHelper.ExecuteReader("")) { ...

  9. MyEclipse中配置自己的JRE和tomcat

    MyEclipse中配置自己的JRE:windows>Preference>java>Installed JREs>Add>Stantard VM>next> ...

  10. OC调用Swift 整理步骤!总结别人的!方便自己查找!

    1. 2. 上面的修改了一个配置项,有一个Product Module Name在后面会使用. 在工程里面点击File/New/File…,选择iOS/Source/Cocoa Touch Class ...