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
  1. 7
    2 1 5 11 5 9 11
output
  1. 9
input
  1. 5
    5 4 3 2 1
output
  1. 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]),少了一层循环

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. using namespace std;
  6. typedef long long ll;
  7. const int N=;
  8. const ll INF=1e19;
  9. int n,k,a[N],m[N];
  10. inline int read(){
  11. int x=,f=;char ch=getchar();
  12. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  13. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  14. return x*f;
  15. }
  16. ll d[N][N],ans=INF;
  17. void dp(){
  18. for(int i=;i<=n;i++){
  19. ll mn=INF;
  20. for(int j=;j<=k;j++){
  21. mn=min(mn,d[i-][j]);
  22. d[i][j]=mn+abs(a[i]-m[j]);
  23. }
  24. }
  25. }
  26.  
  27. int main(int argc, const char * argv[]) {
  28. n=read();
  29. for(int i=;i<=n;i++) a[i]=m[i]=read()-i;
  30. sort(m+,m++n);
  31. k=unique(m+,m++n)-m-;//printf("k %d\n",k);
  32. dp();
  33. for(int i=;i<=k;i++) ans=min(ans,d[n][i]);
  34. cout<<ans;
  35. return ;
  36. }

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. Sublime text 3 快捷键的使用

    快捷键的便捷使用: ctr+shift+n:打开新的sublime text ctr+shift+w:关闭sublime text ctr+o:打开 某个文件 ctrl+n:新建一个文本 ctrl+w ...

  2. windows 80 端口占用

    1. cmd 2. regidit 3. 注册表 KEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\HTTP'右边有一个'start'的DWORD ...

  3. 从0开始学angularjs-笔记03

    大家好,今天上班第一天,可能大家都不是很想上班吧,我也是一样啦---不想上班就来继续写我的angualrjs教程,造福大家吧!! 今天的主要讲解部分有以下几点:1.演示一个完整的项目结构  2.$sc ...

  4. SAP用户权限解剖及自修改

    通常BASIS会使用PFCG做权限管理,时你保存时会产生一个系统外的profile name,记得SU01时用户有profile 和role两栏位吗?它们的关系如何呢? 首先明白几个概念.1.acti ...

  5. myeclipse 2015 CI 16发布【附下载】

    2015升级版再次来袭! 更新日志: Slack Integration 新版本集成了Slack,你只需要注册一个Slack帐号然后就可以发送和接收代码片段.你甚至不需要登录Slack就可以直接在Ec ...

  6. 对抽屉效果几大github第三方库的调研

    在公司项目新版本方案选择中,对主导航中要使用的抽屉效果进行了调研.主要原因是旧的项目中所用的库ECS评价不是很好.现对当下比较火的几大热门抽屉效果的第三方库进行了调研.代码全部选自github 如果你 ...

  7. 一个UILabel不同部分显示不同颜色

    我们直接来看效果图吧: 需求:就是表格cell里面的状态Label,前面的"状态:"是黑色,后面的状态值是红色,他们在同一个Label上,怎么做呢? 解答:真的是会者不难,难者不会 ...

  8. 我的Android第四章:Android的adb命令使用以及SQlite数据库运用

    adb是什么?:adb的全称为Android Debug Bridge,就是起到调试桥的作用.      adb有什么用?:借助adb工具,我们可以管理设备或手机模拟器的状态.还可以进行很多手机操作, ...

  9. android [因为开了刷机精灵等软件 导致adb 无法使用]error: could not install *smartsocket* listener: cannot bind

    今天 使用 刷机精灵后 在使用android studio 时发现 adb 无法正常使用.   于是 想重启 adb.exe , 直接在DOS里杀掉adb输入:adb kill-server 再启动输 ...

  10. C++语言-06-文件操作

    C语言文件操作 C++语言是C语言的超集,是在C语言的基础上增加了面向对象的特性而创造出来的,最初被命名为带类的C.所以C++语言中包含了C语言的思想,如:C++语言中文件操作的原理与步骤与C语言基本 ...