http://codeforces.com/contest/361/problem/D

用二分搜索相邻两个数的差的绝对值,然后用dp记录数改变的次数。dp[i]表示在i之前改变的次数,如果|a[i]-a[j]|<=(i-j)*mid 需要改变。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define LL __int64
using namespace std;
const LL inf=;
int dp[];
int n,k;
LL a[];
LL ABS(LL a)
{
if(a<) return -a;
return a;
}
bool ok(LL c)
{
memset(dp,,sizeof(dp));
for(int i=; i<=n; i++)
{
dp[i]=i;
for(int j=; j<i; j++)
{
if(ABS(a[i]-a[j])<=(LL)(i-j)*c)
dp[i]=min(dp[i],dp[j]+(i-j-));
}
if(dp[i]+(n-i-)<=k) return true;
}
return false;
} int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
for(int i=; i<=n; i++)
{
scanf("%I64d",&a[i]);
}
LL l=,r=inf;
while(l<=r)
{
LL mid=(l+r)/;
if(ok(mid))
{
r=mid-;
}
else l=mid+;
}
printf("%I64d\n",l);
}
return ;
}

cf D. Levko and Array的更多相关文章

  1. cf C. Levko and Array Recovery

    http://codeforces.com/contest/361/problem/C 这道题倒着一次,然后正着一次,在正着的一次的时候判断合不合法就可以. #include <cstdio&g ...

  2. [codeforces 360]A. Levko and Array Recovery

    [codeforces 360]A. Levko and Array Recovery 试题描述 Levko loves array a1, a2, ... , an, consisting of i ...

  3. CF360B Levko and Array (二分查找+DP)

    链接:CF360B 题目: B. Levko and Array time limit per test 2 seconds memory limit per test 256 megabytes i ...

  4. Codeforces 361D Levko and Array(二分)(DP)

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

  5. 有意思的DP(CF360B Levko and Array)

    刚才面试了一个蛮有意思的DP题目,脑子断片,没写出来,不过早上状态还是蛮好的 一个长度为n的序列最多改变k次,使相邻两数之差绝对值的最大值最小 三维的dp我先尝试写一下 Codeforces 360B ...

  6. CodeForces - 361D Levko and Array

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

  7. CF 1008C Reorder the Array

    You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it ...

  8. Codeforces Round #210 (Div. 2) C. Levko and Array Recovery

    题目链接 线段树的逆过程,想了老一会,然后发现应该是包含区间对存在有影响,就不知怎么做了...然后尚大神,说,So easy,你要倒着来,然后再正着来,判断是不是合法就行了.然后我乱写了写,就过了.数 ...

  9. CF 295A Greg and Array (两次建树,区间更新,单点查询)

    #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...

随机推荐

  1. 使用activeMQ实现jms

    一:jms介绍         jms说白了就是java message service,是J2EE规范的一部分,跟jdbc差不多,sun只提供了接口,由各个厂商(provider)来进行具体的实现, ...

  2. FE: Responsive Web Design Overview

    布局特点 1. 单列布局 vs 水平布局 手机屏幕宽度较小,因此多采用单列布局.    反观桌面网页设计,为了利用宽度,往往使用各种水平布局的组件,诸如水平导航栏,水平按钮组,水平分页等.然而水平布局 ...

  3. 黑马程序员_Java_collections and Arrays(工具类)

    collections collections工具类方法 1,static <T extends Comparable<? super T>> void sort(List&l ...

  4. 高德JAVA笔试题

    1.常用的RuntimeException. 2.叙述RuntimeException和一般异常的异同. 3.叙述ArrayList.Vector和LinkedList各自的特点和特性. 4.什么是反 ...

  5. openstacks

  6. Linux开关机命令详解

    Linux系统的开关机主要涉及(shutdown,reboot,poweroff,halt,init)这几条命令,本文对其使用详解如下: 一.命令简介 shutdown,poweroff,reboot ...

  7. XCode 打包问题巧遇

    XCode 打包问题巧遇 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句: ...

  8. Window 10通过网线和Wifi连接树莓派

    几个月前买了个树莓派,扔在一边没有捣鼓,今天搞定了笔记本通过家里的wifi登录树莓派,下面列出设置过程. 实验环境: 网络:只有wifi 材料:笔记本一台(Win10),树莓派一台,EDUP USB无 ...

  9. django: template variable

    模板变量用双大括号显示,如: <title>page title: {{title}}</title> 一 模板中使用变量 继续前面的例子,修改 index.html: < ...

  10. 验证视图状态 MAC 失败。如果此应用程序由网络场或群集承载,请确保 <machineKey>

    转自:http://hi.baidu.com/taotaowyx/blog/item/074bb8d83907bb3233fa1ce6.html 验证视图状态 MAC 失败.如果此应用程序由网络场或群 ...