codeforces D. Levko and Array(二分加dp) 挺好的一个题

D. Levko and Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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: 44444.

In the third sample he can get array: 123456.

题意:  给你n个数 你最多改变其中的k个数使得 两个相邻的数之间的差的绝对值最小。

思路:  二分 答案 (二分任意差的绝对值 看在mid的情况下是否符合情况 如果符合情况 那么r=mid-1  否则l=mid+1 ) 

而在判断的时候就要用到dp   dp[i] 表示在不改动a[i] 的情况下 前i个数需要改动的个数。

首先dp[1]=0;  那么dp[i] =  ?  当然取决于前边的情况  如果到j需要改动dp[j] 个数 那么对于i 就有dp[i]=dp[j]+ i-j-1 ;  表示(j,i)的数都需要改变(开区间)  而改变的条件就是a[i]和

a[j] 的差值 小于等于(i-j)*mid     而是否符合条件只需要看dp[i]+n-i  是否小于等于k  

d代码: 

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define N 2005

using namespace std;

typedef long long ll;

const ll inf =1e15;

int n,k;
ll a[N];
ll dp[N];


int jud(ll num)
{
	memset(dp,inf,sizeof(dp));
	
	dp[1]=0;
	for(int i=2;i<=n;i++)
	{
		dp[i]=i-1;
		for(int j=1;j<i;j++)
		{
			if(abs(a[i]-a[j])<=(i-j)*num)
			{
				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++) scanf("%lld",&a[i]);
	
	ll l ,r,mid;
	l=0; r=inf;
	ll ans=0;
	while(l<=r)
	{
		mid=(l+r)>>1;
		//printf(" mid : %lld\n",mid);
		if(jud(mid))
		{
			ans=mid;
			r=mid-1;
		}
		else l=mid+1;
	}
	
	printf("%lld\n",ans);
	return 0;
} 

/*

20 17
-5 -9 11 -7 -17 -8 0 -14 -20 -15 7 -13 0 -3 -14 0 9 -10 6 -19

5 1
-1000000000 1000000000 -1000000000 1000000000 -1000000000


*/


### Codeforces Div.2 比赛难度介绍 Codeforces Div.2 比赛主要面向的是具有基础编程技能到中级水平的选手。这类比赛通常吸引了大量来自全球不同背景的参赛者,包括大学生、高中生以及一些专业人士。 #### 参资格 为了参 Div.2 比赛,选手的评级应不超过 2099 分[^1]。这意味着该级别的竞赛适合那些已经掌握了一定算法知识并能熟练运用至少一种编程语言的人群参与挑战。 #### 目设置 每场 Div.2 比赛一般会提供五至七道目,在某些特殊情况下可能会更多或更少。这些目按照预计解决难度递增排列: - **简单(A, B 类型)**: 主要测试基本的数据结构操作和常见算法的应用能力;例如数组处理、字符串匹配等。 - **中等偏难(C, D 类型)**: 开始涉及较为复杂的逻辑推理能力和特定领域内的高级技巧;比如图论中的最短路径计算或是动态规划入门应用实例。 - **高难度(E及以上类型)**: 对于这些问,则更侧重考察深入理解复杂概念的能力,并能够灵活组合多种方法来解决问;这往往需要较强的创造力与丰富的实践经验支持。 对于新手来说,建议先专注于理解和练习前几类较容易的问,随着经验积累和技术提升再逐步尝试更高层次的任务。 ```cpp // 示例代码展示如何判断一个数是否为偶数 #include <iostream> using namespace std; bool is_even(int num){ return num % 2 == 0; } int main(){ int number = 4; // 测试数据 if(is_even(number)){ cout << "The given number is even."; }else{ cout << "The given number is odd."; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值