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,你要倒着来,然后再正着来,判断是不是合法就行了.然后我乱写了写,就过了.数 ...
随机推荐
- Java集合---List、Set、Iterator、Map简介
1.List集合 1.1概念 List继承自Collection接口.List是一种有序集合,List中的元素可以根据索引(顺序号:元素在集合中处于的位置信息)进行取得/删除/插入操作. 跟Set集合 ...
- 2612. [FHZOI 2017]被窃的项链
2612. [FHZOI 2017]被窃的项链 ★★★ 输入文件:stolen_necklace.in 输出文件:stolen_necklace.out 简单对比时间限制:1 s 内存 ...
- luogu3338 [ZJOI2014]力
我发现我的构造方法好像不太一样而且比较显然?--先读入 \(q\) 数组(下表从零开始). 记 \(i < j\) 时,\(a_{i-j}=-1/i^2\):\(i > j\) 时,\(a ...
- WWDC2014:留给微软的时间不多了!
业界定律:第一和第二吵架,最受伤的总是第三名.苹果的wwdc和谷歌io大会的在6月相继召开,结果必然会有一番对比互讽.作为一个曾经的c#程序员,看着在角落里不断划圈圈的微软,心里总是不禁想起那句话:留 ...
- Shell脚本直接执行sql语句和不显示列名
在shell脚本编程的时候,可以通过在mysql连接命令添加-N和-e参数实现查询结果不显示列名和直接执行sql语句操作 demo $(mysql -h ${HOST} -u ${USER} -p${ ...
- 后缀数组(SA)学习记录
一只只会后缀自动机却不会后缀数组的弱鸡做了一下HDU - 1403,结果SAM被卡内存了,然后学习了一下SA. 以下两道题都是求LCS,区别在于字符串长度. 参考blog:https://www.cn ...
- sqlserver把bak备份还原到另一个数据库
1.新建数据库aa 2.选择还原数据库,在选项下选择“覆盖现有数据库”即可
- C#中var、int、object性能比较(已修正)
var关键字是.net3.5推出的关键字,主要是让编译器自动推断并判断变量类型,类似javascript中的var. 在使用一些性能优化软件时,在代码优化时,我发现不管定义什么类型的变量,变量的类型都 ...
- 【转】unity自带寻路Navmesh入门教程(一)
http://liweizhaolili.blog.163.com/blog/static/16230744201271161310135/ 说明:从今天开始,我阿赵打算写一些简单的教程,方便自己日后 ...
- mysql导入导出命令详解
mysql导入导出命令详解 该命令适用于临时备份操作. 一.导出数据库用mysqldump命令(注意mysql的安装路径,即此命令的路径): /usr/local/mysql/bin/ ---> ...