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,你要倒着来,然后再正着来,判断是不是合法就行了.然后我乱写了写,就过了.数 ...
随机推荐
- B树、B-树、B+树、B*树之间的关系
https://blog.csdn.net/u013411246/article/details/81088914
- Java并发之(3):锁
锁是并发编程中的重要概念,用来控制多个线程对同一资源的并发访问,在支持并发的编程语言中都有体现,比如c++ python等.本文主要讲解Java中的锁,或者说是重入锁.之所以这么说是因为在Java中, ...
- c++ 中的slipt实现
来自 http://www.cnblogs.com/dfcao/p/cpp-FAQ-split.html http://blog.diveinedu.com/%E4%B8%89%E7%A7%8D%E5 ...
- springmvc支持跨域的请求(复制)
Spring MVC 新增跨域支持 发表于2017/5/8 22:01:24 48人阅读 分类: SpringMVC Spring MVC 4.2 增加 CORS 支持 跨站 HTTP 请求(Cro ...
- 微信Oauth2.0网页开放授权
网页授权获取用户基本信息 如果用户在微信中(Web微信除外)访问公众号的第三方网页,公众号开发者可以通过此接口获取当前用户基本信息(包括昵称.性别.城市.国家).利用用户信息,可以实现体验优化.用户来 ...
- 基于Linux的嵌入式文件系统构建与设计
摘 要:Linux是当今一种十分流行的嵌入式操作系统.由于其具有执行效率高.占用空间小.实时性能优良和可扩展性强等特点,因此被广泛应用于工业控制领域.该文对其文件系统进行了简单的介绍,结合嵌入式系统应 ...
- 0-Android系统各层中LOG的使用
Android系统各层中LOG的使用 , ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */ ANDROID_LOG_VERBOSE, ANDR ...
- Mysql 数据库备份工具 xtrabackup
1.安装测试数据库mysql5.7 详细步骤 yum install -y gcc gcc-c++ cmake bison ncurses-devel .tar.gz cd mysql-/ cmake ...
- Python之面向对象:继承
概念:子类继承父类的属性和方法. 一个派生类(derived class)继承基类(bass class)字段和方法.继承也允许把一个派生类的对象作为一个基类对象对待. 一.单继承 :推崇.特点和使用 ...
- cxGrid让指定的某行自动呈选选中的状态
cxView.ViewData.Rows[cxView.DataController.DataSource.DataSet.RecNo-1].Selected := True;//将当前的行呈选中的状 ...