B. Levko and Array

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

Sample test(s)
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.

//如果mid 可以满足 那么mid+1也是可以满足,这样就可以二分答案、
//这样就可以二分答案、注意二分的最大值要超过 2*10^9
// 然后就可以dp判断这个mid 是否满足
// dp[i] , 表示 计算到 a[i] , a[i] 不变时需要最少的修改次数才可以满足 相邻差 mid
// 转移方程 dp[i] = min(dp[i],dp[j]+(i-j-1)) ; j < i && a[i]-a[j] <= (i-j)*mid ;
// 意思:如果a[i]-a[j] 满足 (i-j)*mid 那么 通过改变所有 j+1~i-1的数是可以满足条件的

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <algorithm>
#define maxn 2002
#define INF 2000000001
#define LL long long
using namespace std;

int n , k ;
LL a[maxn] ;
int dp[maxn] ;
int abs1( int a ){ return a >= 0 ? a : -a ;}
int min1( int a , int b ){ return a > b ? b : a ;}
bool find( LL mid ) 
{
    int i , j , u ;
    for( i = 1 ; i <= n+1 ; i++)
        dp[i] = i-1 ;
        dp[1] = 0 ;
    for( i = 2 ; i <= n ;i++ )
        for( j = 1 ; j < i ;j++ )if(abs1(a[i]-a[j]) <= (i-j)*mid)
             dp[i] = min1(dp[i],dp[j]+(i-j-1)) ;
    for( i = 1 ; i <= n ;i++)
        dp[n+1] = min1(dp[n+1],n-i+dp[i]) ;
    if(dp[n+1] <= k)return true ;
    return false ;
}
int main()
{
    int i ;
    LL mid , L , R ;
    //freopen("in.txt","r",stdin) ;
    while( scanf("%d%d" ,&n , &k ) != EOF )
    {
        for( i = 1 ; i <= n ;i++)
            scanf("%I64d" , &a[i]) ;
        L = 0 ; R = INF ; // 二分答案
        while( L <= R )
        {
            mid = (L+R)>>1 ;
            if(find(mid))R = mid-1 ;
            else L = mid+1 ;
        }
        printf("%d\n",R+1) ;
    }
    return 0 ;
}

 

转载于:https://www.cnblogs.com/20120125llcai/p/3438424.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值