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 ;
}

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

  1. 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 ...

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

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

  3. 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 ...

  4. 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 ...

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

  6. Codeforces Round #371 (Div. 2) C. Sonya and Queries 水题

    C. Sonya and Queries 题目连接: http://codeforces.com/contest/714/problem/C Description Today Sonya learn ...

  7. Codeforces Round #371 (Div. 2) C. Sonya and Queries —— 二进制压缩

    题目链接:http://codeforces.com/contest/714/problem/C C. Sonya and Queries time limit per test 1 second m ...

  8. Codeforces Round #371 (Div. 2) C. Sonya and Queries[Map|二进制]

    C. Sonya and Queries time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. Codeforces Round #371 (Div. 2) C. Sonya and Queries

    题目链接 分析:01trie树,很容易就看出来了,也没什么好说的.WA了一发是因为没有看见如果数字位数大于01序列的时候01序列也要补全0.我没有晚上爬起来打,白天发现过的人极多. /******** ...

随机推荐

  1. go语言选择语句 switch case

    根据传入条件的不同,选择语句会执行不同的语句.下面的例子根据传入的整型变量i的不同而打印不同的内容: switch i { case 0: fmt.Printf("0") case ...

  2. [deviceone开发]-购物车的简单示例

    一.简介 主要是演示listview所在的ui和模板cell所在的ui之间数据的交互,点击一行,可以通过加减数量,自动把所有选中的汽车价格显示在底部. 二.效果图 三.示例地址: http://sou ...

  3. 一次简单的分析手机APK病毒木马

    写个笔记记录一下,起因是朋友在QQ上发了个连接叫我看看 安不安全,叫我帮他看看,反正在店里待着也没生意,那就顺便看看咯. 打开这个网址  会自动弹出下载一个名为OOXX的APK安装包. 起先我的思路是 ...

  4. 关于web软件信息安全问题防护资料的整理(二)

    想要做好软件的安全防护,首先就得了解web系统的安全威胁,那么web系统都存在哪些威胁呢? 应用层攻击.网络层攻击和混合攻击. 传统被动.单点以及彼此孤立的防护手段已不能应对越来越严峻的安全威胁. 改 ...

  5. SharePoint文档库,如何在新窗口打开中的文件

    默认情况下,点击文档库中的文件是在当前浏览器中打开的(如果你设置的是在客户端软件打开,则不符合本文情况).那么如果让他在新窗口中打开呢? 这里需要借助jQuery,关于如何将jQuery集成到Shar ...

  6. 表达式语言EL

    表达式语言EL 表达式语言 EL(Expression Language,表达式语言)主要是用在JSP页面中,用来辅助我们产生无脚本的JSP页面,此处的脚本指的是JSP中的Java代码. EL的语法是 ...

  7. IOS 四舍五入 进一法 去尾法

    float numberToRound; int result; numberToRound = 4.51; result = (int)roundf(numberToRound); NSLog(@& ...

  8. 【代码笔记】iOS-旋转的图片

    一,效果图. 二,工程图. 三,代码. AppDelegate.h #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder ...

  9. 委托 C#

    这些就是自己的理解. 委托的用处就是把方法传递给其他方法. 1委托的使用更类差不多,也 是需要先定义再实例化. 2Action<T>和Func<T>委托 3多播委托 4.匿名方 ...

  10. 荷兰国旗 Flag of the Kingdom of the Netherlands

    问题描述:现有n个红白蓝三种不同颜色的小球,乱序排列在一起,请通过两两交换任意两个球,使得从左至右的球依次为红球.白球.蓝球.这个问题之所以叫做荷兰国旗,是因为将红白蓝三色的小球弄成条状物,并有序排列 ...