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. JVM-ClassLoader(转)

    在加载阶段主要用到的是方法区: 方法区是可供各条线程共享的运行时内存区域.存储了每一个类的结构信息,例如运行时常量池(Runtime Constant Pool).字段和方法数据.构造函数和普通方法的 ...

  2. USBSpirit(USB精灵)更新到1.2.300.105

    USBSpirit(USB精灵)是CopyU!的内核引擎,CopyU!的主要功能均由该引擎提供,此次更新主要内容如下:(版本号:1.2.300.105) 1.[修复]:修复了几处引擎的资源泄露问题,提 ...

  3. HDU_2043——判断密码是否安全

    Problem Description 网上流传一句话:"常在网上飘啊,哪能不挨刀啊-".其实要想能安安心心地上网其实也不难,学点安全知识就可以.首先,我们就要设置一个安全的密码. ...

  4. Raid1源代码分析--写流程

    正确写流程的总体步骤是,raid1接收上层的写bio,申请一个r1_bio结构,将其中的所有bios[]指向该bio.假设盘阵中有N块盘.然后克隆N份上层的bio结构,并分别将每个bios[]指向克隆 ...

  5. SRM 585 DIV1

    A 树形dp是看起来比较靠谱的做法 , 但是转移的时候不全面就会出错 , 从贪心的角度出发 , 首先让第一量车走最长路, 然后就会发现递归结构 , 得到递归式 f[i] = ( f[i-2] + f[ ...

  6. Tradesy | IT桔子

    Tradesy | IT桔子 Tradesy www.tradesy.com   认领 关注 分享

  7. CSS常用操作-对齐

    index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...

  8. 重写boost内存池

    最近在写游戏服务器网络模块的时候,需要用到内存池.大量玩家通过tcp连接到服务器,通过大量的消息包与服务器进行交互.因此要给每个tcp分配收发两块缓冲区.那么这缓冲区多大呢?通常游戏操作的消息包都很小 ...

  9. servletContext百科

    servletContext 编辑   servletContext接口是Servlet中最大的一个接口,呈现了web应用的Servlet视图.ServletContext实例是通过 getServl ...

  10. Android设备中实现Orientation Sensor(图)兼谈陀螺仪

    设备中的三自由度Orientation Sensor就是一个可以识别设备相对于地面,绕x.y.z轴转动角度的感应器(自己的理解,不够严谨).智能手机,平板电脑有了它,可以实现很多好玩的应用,比如说指南 ...