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

input

7
2 1 5 11 5 9 11

output

9

input

5
5 4 3 2 1

output

12

hint


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 5 2 3 6 这里 5变成2最优

1 2 3 10 9 这里最优方案为 9变成10

可以设\(f[i][j]\)表示前i个数不超过j的最小代价

\[f[i][j]=min(f[i][j-1],f[i-1][j]+abs(a[i]-j))
\]

根据我们之前的推论,那么可以把j(1~max(a))转为为已经存在的数字,把a数组排序去重之后得到b数组

原方程优化为

\[f[i][j]=min(f[i][j-1],f[i-1][j]+abs(a[i]-b[j]))
\]

已知最坏情况下是将每个数都转成一个相同的数,而这个数在原数组出现过,那么n个数肯定不会超过max(a)

答案为f[n][tot],总的时间复杂度为\(O(n^2)\)

至于原题要求严格的递增序列,可以根据

\(ai<a[j] \Leftrightarrow ai \leq aj-1\)

让每个ai-i即可转为一个新的数组,这个数组变成非严格递增的代价就是原数组变成严格递增的代价。

import java.io.*;
import java.util.*;
public class Main {
static final int N=(int)3000+5;
static final long inf=(long)3e12;
long []a=new long[N];
long []b=new long[N];
long [][]f=new long[N][N];
public void solve() {
Scanner in=new Scanner(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
int n=in.nextInt();
for(int i=1;i<=n;i++){
a[i]=in.nextInt()-i;
b[i]=a[i];
}
Arrays.sort(b,1,n+1);
int tot=1;
for(int i=2;i<=n;i++) {
if(b[i]!=b[tot]) b[++tot]=b[i];
}
for(int i=0;i<=n;i++) {
Arrays.fill(f[i], inf);
f[0][i]=0;
}
for(int i=1;i<=n;i++) {
for(int j=1;j<=tot;j++) {
f[i][j]=Math.min(f[i][j-1], f[i-1][j]+Math.abs(a[i]-b[j]));
}
}
out.println(f[n][tot]);
out.flush();
in.close();out.close();
}
public static void main(String[] args) {
(new Main()).solve();
}
}

存在一种贪心做法,记录之前出现的大数,如果这个大数大于当前数,就把他变成当前数。

对于这个贪心解法,暂时还没想到证明,先记录一下。

import java.io.*;
import java.util.*;
public class Main {
static final int N=(int)3000+5;
static final int inf=(int)1e9;
public void solve() {
Scanner in=new Scanner(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
PriorityQueue<Integer>que=new PriorityQueue<Integer>(new Comparator<Integer>() {
@Override
public int compare(Integer o1,Integer o2) {
if(o1.equals(o2)) return 0;
return o1.compareTo(o2)>0?-1:1;
}
});
int n=in.nextInt();
long ans=0;int x;
for(int i=1;i<=n;i++) {
x=in.nextInt()-i;
que.offer(x);
if(x<que.peek()) {
ans+=que.poll()-x;
que.offer(x);
}
}
out.println(ans);
out.flush();
in.close();out.close();
}
public static void main(String[] args) {
(new Main()).solve();
}
}

Codeforces C. Sonya and Problem Wihtout a Legend(DP)的更多相关文章

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

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

  2. codeforces C. Sonya and Problem Wihtout a Legend(dp or 思维)

    题目链接:http://codeforces.com/contest/713/problem/C 题解:这题也算是挺经典的题目了,这里附上3种解法优化程度层层递进,还有这里a[i]-i<=a[i ...

  3. Codeforces713C Sonya and Problem Wihtout a Legend(DP)

    题目 Source http://codeforces.com/problemset/problem/713/C Description Sonya was unable to think of a ...

  4. Codeforces 713C Sonya and Problem Wihtout a Legend(单调DP)

    [题目链接] http://codeforces.com/problemset/problem/713/C [题目大意] 给出一个数列,请你经过调整使得其成为严格单调递增的数列,调整就是给某些位置加上 ...

  5. CodeForces 714E Sonya and Problem Wihtout a Legend(单调数列和DP的小研究)

    题意:给你n个数字,每个数字可以加减任何数字,付出变化差值的代价,求最后整个序列是严格单调递增的最小的代价. 首先我们要将这个题目进行转化,因为严格单调下是无法用下面这个dp的方法的,因此我们转化成非 ...

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

  7. Codeforces 713C Sonya and Problem Wihtout a Legend

    题意:给一个序列,可以进行若干次操作,每次操作选择一个数让它+1或-1,问最少要几次操作使得序列变为严格单调递增序列. 题解:首先考虑非严格递增序列,则每个数字最终变成的数字一定是该数组中的某个数.那 ...

  8. 【CF713C】Sonya and Problem Wihtout a Legend(离散化,DP)

    题意:给你一个数列,对于每个数字你都可以++或者−− 然后花费就是你修改后和原数字的差值,然后问你修改成一个严格递增的,最小花费 思路:很久以前做过一道一模一样的 严格递增很难处理,就转化为非严格递增 ...

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

随机推荐

  1. Win10系统特别卡的一个原因

    我遇到的Win10特别卡的原因是它自带的一个杀毒软件 迈克菲(McAfee)导致的,在卸载之前电脑真的特别卡,打开一个窗口都卡,,卸载了之后瞬间感觉电脑飞起来了.... 当然还有很多原因会导致电脑卡, ...

  2. Spark Mllib里如何将trainDara训练数据文件里提取第M到第N字段(图文详解)

    不多说,直接上干货! 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的第13章 使用决策树二元分类算法来预测分类StumbleUpon数据集

  3. CI模板中如何引入模板

    <?php $this->load->view('index/head.html') ?>

  4. json_encode 中文处理

    在 php 中使用 json_encode() 内置函数(php > 5.2)可以使用得 php 中数据可以与其它语言很好的传递并且使用它. 这个函数的功能是将数值转换成json数据存储格式. ...

  5. apache配置优化 - 解决apache环境下网站访问速度慢的问题(重点参考)

    如果apche访问量过大,将会导致页面打开迟缓,下载速度也降低,如果由于经费和环境问题,集群方案没有得以应用.可以通过对Apache2增加模块MPM来进行优化, 这里我选择线程型MPM加以优化:  开 ...

  6. Solr6+IKAnalyzer分词环境搭建

    环境要求 Zookeeper版本:zookeeper-3.4.8 JDK版本: jdk1.8. Solr版本:solr-6.4.1 Tomcat版本:tomcat8 ZK地址:127.0.0.1:21 ...

  7. CF747D Winter Is Coming

     题目链接: http://codeforces.com/problemset/problem/747/D 题目大意: 接下来的n天内每天都有一个气温,如果某天的温度为负数,则必须使用冬季轮胎:而温度 ...

  8. 【学习笔记】六:面向对象的程序设计——理解JS中的对象属性、创建对象、JS中的继承

    ES中没有类的概念,这也使其对象和其他语言中的对象有所不同,ES中定义对象为:“无序属性的集合,其属性包含基本值.对象或者函数”.现在常用的创建单个对象的方法为对象字面量形式.在常见多个对象时,使用工 ...

  9. [原创] SOAP UI 创建SOAP工程进行接口测试

    下载及安装 1. 登录http://www.soapui.org/ 2. 鼠标移动到导航头的Downloads选项 3. 点击SOAP UI 4. 下载页面 新建项目 创建项目 1. 创建项目很简单. ...

  10. 修改完linux bashrc文件之后,如何不重启系统使其生效

    修改完后,输入如下命令即可 ##@##:~/    source ~/.bashrc 之后bashrc文件就可以使用! 注: 使用ssh登陆shell的时候,系统不会自动调用.bashrc文件, 只是 ...