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

时间:2023-01-31 00:24:03
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 &num;371 &lpar;Div&period; 1&rpar; C&period; Sonya and Problem Wihtout a Legend 贪心

    C. Sonya and Problem Wihtout a Legend 题目连接: http://codeforces.com/contest/713/problem/C Description ...

  2. Codeforces Round &num;371 &lpar;Div&period; 1&rpar; C - Sonya and Problem Wihtout a Legend

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

  3. codeforces 713C C&period; Sonya and Problem Wihtout a Legend&lpar;dp&rpar;

    题目链接: 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&period; Sonya and Problem Wihtout a Legend&lpar;dp&rpar;(将一个数组变成严格单增数组的最少步骤)

    E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...

  6. Codeforces Round &num;371 &lpar;Div&period; 2&rpar; C&period; Sonya and Queries 水题

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

  7. Codeforces Round &num;371 &lpar;Div&period; 2&rpar; C&period; Sonya and Queries —— 二进制压缩

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

  8. Codeforces Round &num;371 &lpar;Div&period; 2&rpar; C&period; Sonya and Queries&lbrack;Map|二进制&rsqb;

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

  9. Codeforces Round &num;371 &lpar;Div&period; 2&rpar; C&period; Sonya and Queries

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

随机推荐

  1. iOS 陀螺仪,加速度计

    atan2(x, y)反正切函数  x是对边  y临边 data.acceleration  加速度值,当手机水平放置时值为(0,0,-1),当手机竖直放置时值为(0,-1,0),判断手机拿起状态可以 ...

  2. Java&colon; RandomAccessFile

    RandeomAccessFile虽然是java.io中的成员,但该类只继承了Object类,实现了DataOut, DataIn, Closeable等接口.此类的实例支持对文件的随机访问和读取.随 ...

  3. Eclipse 关于&OpenCurlyDoubleQuote;The type &ast; is not accessible due to restriction on required library”问题的解决办法

    The type * is not accessible due to restriction on required library”的错误, 意思是所需要的类库由于受限制无法访问. 解决办法: 1 ...

  4. spring为什么不能注入static变量

    Spring 依赖注入 是依赖 set方法 set方法是 是普通的对象方法 static变量是类的属性 @Autowired private static JdbcTemplate jdbcTempl ...

  5. 当 IDENTITY&lowbar;INSERT 设置为 OFF 时,不能向表 中的标识列插入显式值错误的解决方法

    一个主键.两个外键,把两个外键改为非空就行了. CREATE TABLE [dbo].[User_Compare]( ,) NOT NULL, [UserId] [int] NOT NULL, [Pa ...

  6. MS数据库优化查询最常见的几种方法

    1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计算列导致查询不优化. 4.内存不足 5.网络速度慢 6.查询出的数据量过大 ...

  7. 001&lowbar;python实现数据分析

    一. # coding:utf8 # !/usr/bin/python # import numpy as np import pandas as pd import np def example2( ...

  8. &lbrack;转帖&rsqb;利用hydra(九头蛇)暴力破解内网windows登录密码

    利用hydra(九头蛇)暴力破解内网windows登录密码 https://blog.csdn.net/weixin_37361758/article/details/77939070 尝试了下 能够 ...

  9. Linus运行jar包的操作

    cd /    返回最顶层文件夹cd home/numa        进入home下的numa文件夹ll         查看当前文加夹下的所有文件ps -ef | grep java        ...

  10. url提交参数类

    url提交参数类 type /// <summary> /// 准备url /// </summary> TynUrl = class private FUrl, FComma ...