CodeForces - 361D Levko and Array
Discription
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
5 2
4 7 4 7 4
0
3 1
-100 0 100
100
6 3
1 2 3 7 8 9
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就行啦,水水水hhhh
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=20005,inf=2*1e9;
int n,a[maxn],f[maxn],k,mid;
inline int mabs(int x){ return x<0?-x:x;} inline bool solve(){
for(int i=1;i<=n;i++){
f[i]=i-1;
for(int j=1;j<i;j++) if(mabs(a[i]-a[j])<=(i-j)*(double)mid) f[i]=min(f[i],f[j]+i-j-1);
if(f[i]+n-i<=k) return 1;
} return 0;
} int main(){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) scanf("%d",a+i); int l=0,r=inf,ans;
while(l<=r){
mid=(l+(ll)r)>>1;
if(solve()) r=mid-1,ans=mid;
else l=mid+1;
} printf("%d\n",ans);
return 0;
}
CodeForces - 361D Levko and Array的更多相关文章
- 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 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 442C Artem and Array(stack+贪婪)
题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...
- Codeforces Round #504 D. Array Restoration
Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...
- codeforces 361 D. Levko and Array(dp+二分)
题目链接:http://codeforces.com/contest/361/problem/D 题意:最多可以修改K次数字,每次修改一个数字变成任意值,C=max(a[i+1]-a[i]):求操作之 ...
- codeforces 361 C. Levko and Array Recovery(暴力+思维)
题目链接:http://codeforces.com/contest/361/problem/C 题意:对一个数列有这么两个操作 1.(1,l,r,p)..将区间[l,r]所有数都加上p 2.(2,l ...
- Codeforces Round #210 (Div. 2) C. Levko and Array Recovery
题目链接 线段树的逆过程,想了老一会,然后发现应该是包含区间对存在有影响,就不知怎么做了...然后尚大神,说,So easy,你要倒着来,然后再正着来,判断是不是合法就行了.然后我乱写了写,就过了.数 ...
随机推荐
- MySQL安装教程&Navicat安装
一.下载MySQL http://jingyan.baidu.com/article/e3c78d64412ae83c4c85f5fd.html 首先打开MySQL官网,找到Downloads标签,点 ...
- TCP报文格式,TCP的三次握手和四次挥手&hosts文件
1.TCP报文格式 TCP报头中的源端口号和目的端口号同IP数据报中的源IP与目的IP唯一确定一条TCP连接 序号(4字节=32位): 37 59 56 75 用来标识TCP发端向TCP收端发送的数据 ...
- js数据类型的检测总结,附面试题--封装一个函数,输入任意,输出他的类型
一.javascript 中有几种类型的值 1.基本数据类型 : 包括 Undefined.Null.Boolean.Number.String.Symbol (ES6 新增,表示独一无二的值) 特点 ...
- WebApp开发技巧
http://www.cnblogs.com/WhiteCusp/p/4502961.html http://ju.outofmemory.cn/entry/25675 http://www.fron ...
- Python+Selenium练习篇之17-断言页面标题
继续来介绍一个Selenium中页面title断言方法. 相关脚本代码如下: # coding=utf-8 import time from selenium import webdriver dri ...
- LeetCode with Python -> String
344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...
- csapp 深入理解计算机系统 csapp.h csapp.c文件配置
转载自 http://condor.depaul.edu/glancast/374class/docs/csapp_compile_guide.html Compiling with the CS ...
- maven学习(十一)——maven中的聚合与继承
一.聚合 如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合 1.1.聚合配置代码 <modules> <module>模块一</module> & ...
- jquery的html、text、val的用法
.html()用为读取和修改元素的HTML标签 .text()用来读取或修改元素的纯文本内容 .val()用来读取或修改表单元素的value值. 这三个方法功能上的对比 .html(),.text() ...
- sun.misc.BASE64Encoder()编码有换行符需要手动去除passwordEncode.replace("\n","");
String passwordEncode = new BASE64Encoder().encodeBuffer(password.getBytes());//sun.misc.BASE64Encod ...