zoukankan      html  css  js  c++  java
  • Codeforces Round #210 (Div. 1) B 二分+dp

    B. Levko and Array
     
    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
    Copy
    5 2
    4 7 4 7 4
    output
    Copy
    0
    input
    Copy
    3 1
    -100 0 100
    output
    Copy
    100
    input
    Copy
    6 3
    1 2 3 7 8 9
    output
    Copy
    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.

    二分+DP问题

    ok(x) 表示max(abs(a[i]-a[i+1])) = x 时修改的次数是否小于等于K

    ans[i] 表示前i个数最少修改多少使的相差小于x
    当a[i]-a[j] <= x(i-j)时 表示可以不修改第i个和第j个,修改j-i中间的数就能使的
    相差小于x了。

     1 #include <bits/stdc++.h>
     2 #define ll long long
     3 using namespace std;
     4 const int N = 2020;
     5 ll a[N], ans[N];
     6 ll n, k;
     7 bool ok(ll x) {
     8     ll res = N;
     9     for(ll i = 1; i <= n; i ++) {
    10         ans[i] = i-1;
    11         for(ll j = 1; j < i; j ++) {
    12             if(abs(a[i]-a[j]) <= x*(i-j))
    13                 ans[i] = min(ans[i], ans[j]+i-j-1);
    14         }
    15         res = min(res,ans[i]+n-i);
    16     }
    17     return res <= k;
    18 }
    19 int main() {
    20     cin >> n >> k;
    21     for(int i = 1; i <= n; i ++) cin >> a[i];
    22     ll l = 0, r = 2e9;
    23     while(l < r) {
    24         ll m = (l+r) >> 1;
    25         if(ok(m)) r = m;
    26         else l = m+1;
    27     }
    28     printf("%lld
    ",r);
    29     return 0;
    30 }
  • 相关阅读:
    在ASP.Net和IIS中删除不必要的HTTP响应头
    java合并多个word 2007 文档 基于docx4j
    [转]怎样与 CORS 和 cookie 打交道
    css 设置div半透明 悬浮在页面底部 不随滚动条滚动
    [转]理解Vuex的辅助函数mapState, mapActions, mapMutations用法
    [转]Vue中用props给data赋初始值遇到的问题解决
    [转]import xxx from 和 import {xxx} from的区别
    [转]详解vue父组件传递props异步数据到子组件的问题
    [转]js判断数据类型的四种方法
    [转]iview的render函数用法
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/8906332.html
Copyright © 2011-2022 走看看