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. Linux操作系统分析 ------------------中国科技大学

    http://teamtrac.ustcsz.edu.cn/wiki/Linux2014

  2. CentOS7上GitLab的使用

    生成SSH Keys 生成root账号的ssh key # ssh-keygen -t rsa -C "admin@example.com" 显示pub key的值 # cat ~ ...

  3. Spring Mvc session拦截器实现

    Spring Mvc拦截器实现session过期跳转到登录页面 配置拦截器 <mvc:interceptors> <mvc:interceptor> <mvc:mappi ...

  4. linux下安装apache2.2.27

    1.首先下载httpd-2.2.27.tar.gz用linux命令下载 wget http://mirrors.cnnic.cn/apache//httpd/httpd-2.2.27.tar.gz 2 ...

  5. 多线程(Thread),其实很简单!

    目录:  1:线程简介 2:怎么操作线程      3:Thread的常用方法 4:简单的获奖机     5:应用程序域   线程:是Windows任务调度的最小单位.线程是程序中的一个执行流,每个线 ...

  6. 多维数组遍历PHP

    原文出处 <?php /* * ------------------------------------------------- * Author : nowamagic * Url : ww ...

  7. 【锋利的jQuery】学习笔记03

    第三章 jQuery中的DOM操作 一.DOM操作的分类 DOM(document object model)是一种与浏览器.平台.语言无关的接口,使用该接口可以访问页面中的·所有组件.DOM的操作可 ...

  8. struts 2读书笔记-----struts2的开发流程

    一.将struts 2的lib文件夹下地commons-fileupload.jar.commons-io.jar.freemarker.jar.javassist.jar.ognl.jar.stru ...

  9. 我对WindowsPhone8的一些反大众看法.

    最近看到网上好多人抱怨wp8的系统设计.我个人有一些观点.实在是不吐不快! 1.为什么系统没有重力感应开关.   答:因为这部分开关功能不是wp8所必备的.只是APP的一个功能.所以.错误在APP上因 ...

  10. .NET多线程同步方法详解

    .NET多线程同步方法详解(一):自由锁(InterLocked) .NET多线程同步方法详解(二):互斥锁(lock) NET多线程同步方法详解(三):读写锁(ReadWriteLock) .NET ...