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=;
const ll INF=1e19;
int n,k,a[N],m[N];
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
ll d[N][N],ans=INF;
void dp(){
for(int i=;i<=n;i++){
ll mn=INF;
for(int j=;j<=k;j++){
mn=min(mn,d[i-][j]);
d[i][j]=mn+abs(a[i]-m[j]);
}
}
} int main(int argc, const char * argv[]) {
n=read();
for(int i=;i<=n;i++) a[i]=m[i]=read()-i;
sort(m+,m++n);
k=unique(m+,m++n)-m-;//printf("k %d\n",k);
dp();
for(int i=;i<=k;i++) ans=min(ans,d[n][i]);
cout<<ans;
return ;
}

最新文章

  1. [LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历
  2. Android中分页滑动实现总结
  3. directX基础学习系列7 网格(自己创建)
  4. [经典算法] 排列组合-N元素集合的M元素子集
  5. ios iap 购买总是提示继续的解决方案
  6. Shell:sed流编辑器
  7. 用消息在Win32控制台程序多线程间进行通讯
  8. P酱的冒险旅途(思维)
  9. Lua 服务器Socket通信实例(转)
  10. CentOS 7 多网卡绑定
  11. Unity Shader入门教程(二)最基本的Diffuse和Normal样例
  12. 关于echarts
  13. 22 , CSS 构造颜色、背景与图像
  14. sparksql错误报No such file or director
  15. 原码、补码、反码的概念和java数的存储方式
  16. select2 清除选中项解决办法
  17. CentOS安装redis-audit 但执行时出错未解决 记录一下安装过程
  18. 第三周作业HAproxy文件操作
  19. Digital Roots—HDU1013 2016-05-06 10:25 85人阅读 评论(0) 收藏
  20. linux2.6.30.4内核移植(1)

热门文章

  1. HTML5 表单新增属性
  2. DevExpress VCL 13.1.4支持Delphi /C++Builder XE5
  3. DirectX基础 常用函数语句
  4. AndroidAnnotations简单示例
  5. 01_iOS开发需要准备什么?
  6. 【即时通讯】XMPP调试与简单使用
  7. TFS2012 服务器安装
  8. git入门学习(二):新建分支/上传代码/删除分支
  9. 解决easy ui 1.4datebox控件不能清空的问题
  10. SQL Server ---(CDC)监控表数据(转译)