zoukankan      html  css  js  c++  java
  • 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
    ",R+1) ;
        }
        return 0 ;
    }
    
  • 相关阅读:
    简单的登录验证小程序_python
    远程执行命令_python
    远程执行本地脚本_linux
    反射_python
    ssh oa项目介绍
    返回上一级过程
    ssh框架开发crm(客户关系系统总结)
    struct相对路径,绝对路径
    <s:textfield>标签回显
    ssh框架整合
  • 原文地址:https://www.cnblogs.com/20120125llcai/p/3438424.html
Copyright © 2011-2022 走看看