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. Linux 安装字体

    把XP下的字体C:\WINDOWS\FONTS\simsun.ttc(也就是宋体,大小为10M),把他重命名为 simsun.ttf 拷贝simsun.ttf 字体到 /usr/share/fonts ...

  2. oschina服务器软件

    服务器软件 74Apache模块 54Nginx扩展模块 13Radius相关 94PaaS 系统 29服务发现/注册和协调 17Docker 扩展 7Docker 映像 83应用服务器 189HTT ...

  3. M0、M1、M2、M3都是用来反映货币供应量的重要指标

    m2-反映货币供应量的重要指标编辑词条m2广义货币是一个经济学概念,和狭义货币相对应,货币供给的一种形式或口径,以M2来表示,其计算方法是交易货币(M1,即社会流通货币总量加上活期存款)以及定期存款与 ...

  4. MongoDB开发学习(1)开天辟地,经典入门

    原文地址:http://www.cnblogs.com/xumingxiang/archive/2012/04/08/2437468.html 如果你从来没有接触MongoDB或对MongoDB有一点 ...

  5. HDU_2018——母牛产小牛的问题,递推

    Problem Description 有一头母牛,它每年年初生一头小母牛.每头小母牛从第四个年头开始,每年年初也生一头小母牛.请编程实现在第n年的时候,共有多少头母牛?   Input 输入数据由多 ...

  6. 写两个线程,一个对n每次加一,一个对n每次减一

    public class A{     private static Integer n = 0; }     public class B extends A implements Runnable ...

  7. Storm拓扑的并行度(parallelism)介绍

    Storm拓扑的并行度(parallelism)介绍 1.Storm分为3个主要实体,用于在Storm集群中运行拓扑        工作进程:Worker Process,也称为Worker      ...

  8. svn出现“Previous operation has not finished; run 'cleanup' if it was interrupted”,解决方法

    1.首先不需要动svn的服务器端.2.在客户端安装svn的客户端工具,自定义工具中为:command line client tools    安装完之后,在本地目录有svn.exe执行程序3.然后c ...

  9. Objective-C基础学习笔记——对象初始化

    obj中创建新对象有两种方式:[classname new]和[[classname alloc] init].两种方法等价,Cocoa惯例是使用alloc和init. 1.分配对象: allocat ...

  10. [ES6] Function Params

    1. Default Value of function param: The function displayTopicsPreview() raises an error on the very ...