链接:CF360B

题目:

B. Levko and Array
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Sample test(s)
Input
5 2 4 7 4 7 4
Output
0
Input
3 1 -100 0 100
Output
100
Input
6 3 1 2 3 7 8 9
Output
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.

大意:在数组a中改变任意k个元素的值,使得到的的值最小。

题解:

二分答案,用dp检查可不可行。

dp[j]表示a[0]~a[j]中,a[j]不变时所要改变的数的数量。

 bool check(ll x)
{
int i,j;
for(i=; i<n; i++)
dp[i]=i;
for(i=; i<n; i++)
for(j=i+; j<n; j++)
if(abs(a[i]-a[j])<=(j-i)*x) dp[j]=min(dp[j],dp[i]+j-i-);
for(i=; i<n; i++)
if (dp[i]+n-i- <= k)
return true;
return false;
}

check如上,转移方程时假设a[i]和a[j]之间的数都要改变,那个if是判断是否全部改变了,达成一个超碉的阶梯状,a[i]和a[j]还是差的太远的话就不转移了。

然后后面算的dp[i]+n-i-1是a[i]不变,后面的元素全变的需要变的元素数量。只要有一种方案行,就行。

代码:

 #include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll __int64 int a[];
int dp[];
int n,k;
bool check(ll x)
{
int i,j;
for(i=; i<n; i++)
dp[i]=i;
for(i=; i<n; i++)
for(j=i+; j<n; j++)
if(abs(a[i]-a[j])<=(j-i)*x) dp[j]=min(dp[j],dp[i]+j-i-);
for(i=; i<n; i++)
if (dp[i]+n-i- <= k)
return true;
return false;
} int main()
{
int i;
ll mid,l,r;
while(scanf("%d%d",&n,&k)!=EOF)
{
for(i=; i<n; i++)
scanf("%d",&a[i]);
l=;
r=;
while(l<=r)
{
mid=(l+r)>>;
//cout<<l<<'<'<<mid<<'<'<<r<<endl;
if(check(mid)) r=mid-;
else l=mid+;
}
printf("%I64d\n",l);
}
return ;
}

CF360B Levko and Array (二分查找+DP)的更多相关文章

  1. 有意思的DP(CF360B Levko and Array)

    刚才面试了一个蛮有意思的DP题目,脑子断片,没写出来,不过早上状态还是蛮好的 一个长度为n的序列最多改变k次,使相邻两数之差绝对值的最大值最小 三维的dp我先尝试写一下 Codeforces 360B ...

  2. Leetcode 153. Find Minimum in Rotated Sorted Array -- 二分查找的变种

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  3. 33. Search in Rotated Sorted Array(二分查找)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  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. hdu2993之斜率dp+二分查找

    MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  6. (二分查找 结构体) leetcode33. Search in Rotated Sorted Array

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  7. (二分查找 拓展) leetcode 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  8. leetcode 二分查找 Search in Rotated Sorted Array

    Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions Suppose ...

  9. Search in Rotated Sorted Array(二分查找)

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. Java系列: 关于HttpSessionListener的sessionDestroyed什么时候触发

    根据书本写了下面这个监听器,然后开始调试,打开一个浏览器来访问该网页,可以正常触发sessionCreated,然后关闭浏览器,发现没有触发sessionDestroyed,然后我怀疑是不是这个监听器 ...

  2. Linux(10.18-10.25)学习笔记

    一.学习目标 1. 了解常见的存储技术(RAM.ROM.磁盘.固态硬盘等) 2. 理解局部性原理 3. 理解缓存思想 4. 理解局部性原理和缓存思想在存储层次结构中的应用 5. 高速缓存的原理和应用 ...

  3. Jenkins进阶系列之——16一个完整的JENKINS下的ANT BUILD.XML文件

    网上看见的,确实很全,该有的基本都覆盖到了.自己拿来稍微改改就可以用了. 注:property中的value是你自己的一些本地变量.需要改成自己的 <?xml version="1.0 ...

  4. Android中RelativeLayout属性详细说明

    android:layout_above="@id/xxx"  --将控件置于给定ID控件之上android:layout_below="@id/xxx"  - ...

  5. 闭包拾遗 & 垃圾回收机制

    闭包拾遗 之前写了篇<闭包初窥>,谈了一些我对闭包的浅显认识,在前文基础上,补充并且更新些对于闭包的认识. 还是之前的那个经典的例子,来补充些经典的解释. function outerFn ...

  6. 在.net中为什么第一次执行会慢?

    众所周知.NET在第一次执行的时比第二第三次的效率要低很多,最常见的就是ASP.NET中请求第一个页面的时候要等上一段时间,而后面任意刷新响应都非常迅速,那么是什么原因导致的呢?为什么微软不解决这个问 ...

  7. Windows下apache php wordpress配置

    2. Use notepad to open httpd.conf config file. Make use the line "LoadModule rewrite_module mod ...

  8. use case

  9. MyEclipse 启动报错:'Building workspace' has encountered a problem解决方法

    转载于:http://blog.csdn.net/v123411739/article/details/42645159 每次MyEclipse工作空间报错如下:'Building workspace ...

  10. Java异常分类

    一.基本概念 看java的异常结构图 Throwable是所有异常的根,java.lang.ThrowableError是错误,java.lang.ErrorException是异常,java.lan ...