题干:

Levko has an array that consists of integers: a1, a2, ... , an. But he doesn’t like this array at all.

Levko thinks that the beauty of the array a directly depends on value c(a), which can be calculated by the formula:

The less value c(a) is, the more beautiful the array is.

It’s time to change the world and Levko is going to change his array for the better. To be exact, Levko wants to change the values of at most k array elements (it is allowed to replace the values by any integers). Of course, the changes should make the array as beautiful as possible.

Help Levko and calculate what minimum number c(a) he can reach.

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2000). The second line contains space-separated integers a1, a2, ... , an ( - 109 ≤ ai ≤ 109).

Output

A single number — the minimum value of c(a) Levko can get.

Examples

Input

5 2
4 7 4 7 4

Output

0

Input

3 1
-100 0 100

Output

100

Input

6 3
1 2 3 7 8 9

Output

1

Note

In the first sample Levko can change the second and fourth elements and get array: 4, 4, 4, 4, 4.

In the third sample he can get array: 1, 2, 3, 4, 5, 6.

题目大意:

给一个n个元素的数组: a1, a2, ... , an。允许修改K个元素,使得max(abs(a[i]-a[i+1]))最小。(n<=2000)

Input

第一行输出两个整数n and k (1 ≤ k ≤ n ≤ 2000). 第二行输入n个整数a1, a2, ... , an ( - 109 ≤ ai ≤ 109).

Output

输出相邻两个元素之差的最小值

解题报告:

首先这个题肯定具有单调性,所以二分这个最小值,然后check是否可以在修改K个数以内来满足要求。

check(x)代表最小值数x的时候是否可以在修改K个数以内满足要求,也就是check中的相邻数的差的绝对值必须要<=x才行。check的时候选择dp。dp[i]代表在第i个数不变的情况下,前i个数满足相邻数<=x的最少改变的数。转移就是枚举前面每一个数当上一个不变的数,那中间的数都是变的,注意可以更新的条件,是中间的数都贪心的去这个最大值时,是否可行,你假设x是10,然后a[i-1]是1,a[i]是1e9,那怎么也转移不了对不对,所以贪心的策略,最大差值情况是刚好取公差为x的等差数列。然后取个最小值,并且默认在此条件下是否可以满足条件,也就是后面的数也全改变的情况下是否满足条件,满足直接return 1即可。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e3 + 5;
int dp[MAX];
ll a[MAX];
int n,k;
bool ok(ll x) {for(int i = 1; i<=n; i++) {dp[i] = i-1;for(int j = 1; j<i; j++) {if(abs(a[i] - a[j]) <= (i-j)*x) dp[i] = min(dp[i],dp[j]+(i-j-1));}if(dp[i] + (n-i) <= k) return 1;}return 0;
}int main()
{cin>>n>>k;for(int i = 1; i<=n; i++) cin>>a[i];ll l = 0,r = 2e9,mid,ans;while(l<=r) {mid = (l+r)>>1;if(ok(mid)) r = mid-1,ans = mid;else l = mid+1;} printf("%lld\n",ans);return 0 ;
}

【CodeForces - 361D】Levko and Array (二分,dp)相关推荐

  1. codeforces 361 D. Levko and Array(dp+二分)

    题目链接:http://codeforces.com/contest/361/problem/D 题意:最多可以修改K次数字,每次修改一个数字变成任意值,C=max(a[i+1]-a[i]):求操作之 ...

  2. CodeForces - 1480D2 Painting the Array II(dp)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,现在要求拆分成两个子序列,使得两个子序列的贡献之和最 小.对于一个序列的贡献就是,去掉相邻且相同的字母后的长度,即 ∑i=1n[a[i]! ...

  3. CodeForces 360A - Levko and Array Recovery (模拟)

    题目地址:点击打开链接 思路: 维护每个数的上下界,先按照操作倒着求一遍,可以得到每个数的上界,再按操作正着验证一遍是否能够满足.如果都能满足的话输出上界即可. 代码: #include<ios ...

  4. CodeForces 360A - Levko and Array Recovery (思维)

    题意: 给定一个序列,然后对其进行两种操作 1   L R W   代表从 a[L ] 到a[R] 全部加上W 2   L R W  代表从  a[L] 到 a[R] 中最大值为 W 问是否存在一个序 ...

  5. CodeForces 360A - Levko and Array Recovery【动规】

    题意:一道已知操作求原始数组的题目: 分析:求出每个位置初始时可能的最小值...然后再判断是否合法即可: 疑问之处:为什么a数组初始要给10^9(原始数组的最大范围),而不能是inf? AC代码: # ...

  6. CodeForces 360A - Levko and Array Recovery 给出操作求原始数列

    题意: 对一个数列有这么两个操作 1.(1,l,r,p)..将区间[l,r]所有数都加上p 2.(2,l,r,m).求出区间[l,r]的最大值为m 现在告诉这么一些操作(<5000个)...问能 ...

  7. Codeforces 264B Good Sequences ★ (分解素因子+DP)

    题目链接:http://codeforces.com/problemset/problem/264/B 题目大意:给定一个数列a1,a2,a3,a4,--,an(数据保证ai严格递增,n<=10 ...

  8. 【bzoj1044】[HAOI2008]木棍分割 二分+dp

    题目描述 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍被分成了很多段,要求满足总长度最大的一段长度最小, 并且 ...

  9. CodeForces 1514A Perfectly Imperfect Array

    CodeForces 1514A Perfectly Imperfect Array 题意: 给你n个数,是否存在一个数不是平方数 题解: 先开方,转int,判断是否等于平方 代码: #include ...

  10. Codeforces 148D. Bag of mice(概率dp)

    Codeforces 148D. Bag of mice(概率dp) Description The dragon and the princess are arguing about what to ...

最新文章

  1. MYSQL服务的极简免配置快绿色速安装法[适合新手和懒人]
  2. 最详细的IDEA中使用Debug教程
  3. java springboot b2b2c shop 多用户商城系统源码 (二): 配置管理...
  4. POJ-2524-Ubiquitous Religions
  5. Oracle-使用切片删除的方式清理非分区表中的超巨数据
  6. Spring.Web.Mvc 注入(控制器属性注入)
  7. 虚拟机克隆Linux操作系统后解决MAC地址冲突
  8. 用JavaScript中的示例进行fill()函数
  9. docker安装rabbitmq步骤
  10. C语言之一些值得被定义为常用C语言头文件库的漂亮宏定义
  11. 数学分析笔记—python基础语法
  12. 【数据结构和算法笔记】:图的深度优先搜索(DFS)
  13. QT语言开发的软件界面UI自动化方法
  14. outlook分组邮件提醒
  15. 古文觀止卷七_獲麟解_韓愈
  16. html 抽签分小组代码,javascript随机抽签程序
  17. 爆笑的虫子机器人_《爆笑虫子Larva》全集目录
  18. CAPM与多因子定价模型
  19. 房屋翻新步骤有哪些?极家装修怎么样?
  20. “办公自动化(OA)系统解决方案集”上线

热门文章

  1. Java中getResource()的用法
  2. ConfigurationManager.AppSettings[] ConfigurationManager智能显示不出来
  3. jQuery 实现一个简单的信息反馈或者信息收集的页面
  4. boost helloworlld
  5. 搭建TFS2008的过程及其注意事项
  6. “启动Word时提示出错,只能用安全模式才能打开”的解决方法
  7. [周赛][Leetcode][第5457题][JAVA][动态规划][和为奇数的子数组数目]
  8. [Leedcode][JAVA][第5题][最长回文子串][数组][动态规划]
  9. elementui图片上传php,vue+element-ui+富文本————图片上传
  10. php全局cors,PHP开启CORS - slagga的个人页面 - OSCHINA - 中文开源技术交流社区