cf D. Levko and Array
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的更多相关文章
- cf C. Levko and Array Recovery
http://codeforces.com/contest/361/problem/C 这道题倒着一次,然后正着一次,在正着的一次的时候判断合不合法就可以. #include <cstdio&g ...
- [codeforces 360]A. Levko and Array Recovery
[codeforces 360]A. Levko and Array Recovery 试题描述 Levko loves array a1, a2, ... , an, consisting of i ...
- CF360B Levko and Array (二分查找+DP)
链接:CF360B 题目: B. Levko and Array time limit per test 2 seconds memory limit per test 256 megabytes i ...
- Codeforces 361D Levko and Array(二分)(DP)
Levko and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- 有意思的DP(CF360B Levko and Array)
刚才面试了一个蛮有意思的DP题目,脑子断片,没写出来,不过早上状态还是蛮好的 一个长度为n的序列最多改变k次,使相邻两数之差绝对值的最大值最小 三维的dp我先尝试写一下 Codeforces 360B ...
- CodeForces - 361D Levko and Array
Discription Levko has an array that consists of integers: a1, a2, ... , an. But he doesn’t like this ...
- CF 1008C Reorder the Array
You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it ...
- Codeforces Round #210 (Div. 2) C. Levko and Array Recovery
题目链接 线段树的逆过程,想了老一会,然后发现应该是包含区间对存在有影响,就不知怎么做了...然后尚大神,说,So easy,你要倒着来,然后再正着来,判断是不是合法就行了.然后我乱写了写,就过了.数 ...
- CF 295A Greg and Array (两次建树,区间更新,单点查询)
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...
随机推荐
- 安卓应用使用QQ登录的申请流程
“QQ互联”是腾讯为第三方网站.媒体.终端提供的开放平台.QQ互联拥有8个组件,提供诸如分享.登陆.like.qq提醒等能力.开发者使用QQ帐号登陆组件可以降低了用户的注册门槛,减少注册环节的用户流失 ...
- curl http认证
有些站点需要http认证.(apache认证:http://blog.csdn.net/zf213/article/details/4252592) 如果访问http://test:123789@xx ...
- ffdshow 源代码分析1 : 整体结构
ffdshow是一个非常强大的DirectShow解码器,封装了ffmpeg,libmpeg2等解码库.它也提供了丰富的加工处理选项,可以锐化画面,调节画面的亮度等等.不止是视频,FFDShow现在同 ...
- 总结:man, info, hwclock, cal, type, which, dirname, basename, who, which, whatis, makewhatis, hash, tty
man, info, help, date, hwclock, cal, shutdown, reboot, halt, poweroff, type, cd, which, dirname, bas ...
- c语言内存模型
文章一.C语言的内存分配模型 1.程序代码区:存放函数体的二进制代码. 2.全局区数据区:全局数据区划分为三个区域.全局变量和静态变量的存储是放在一块的,初始化的全局变量和静态变量在一块区域,未初始化 ...
- Android软件的国际化
软件的国际化指的就是:在不同语言的环境的操作系统下,显示不同的语言 2 其实实现软件的国际化很简单: 3 4 1.如果是对文字的国际化,只需要在res文件夹下面建立如下文件夹: 5 values-zh ...
- [LeetCode] 55. Jump Game 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- UVA 11636 - Hello World! 水
水题,贪心,dp都随意 /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,ple ...
- Python偏函数实例
目标: 1.编写一个gui,生成按钮 2.通过偏函数,生成按钮 3.通过装饰器,实现按钮输出信息功能 1.使用Tkinter,创建一个按钮 代码如下: handetiandeMacBook-Pro:~ ...
- mongodb的应用场景
这篇文章总结的比较到位:http://www.tuicool.com/articles/YnmaAj