CF360B Levko and Array (二分查找+DP)
链接:CF360B
题目:
2 seconds
256 megabytes
standard input
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.
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).
A single number — the minimum value of c(a) Levko can get.
5 2 4 7 4 7 4
0
3 1 -100 0 100
100
6 3 1 2 3 7 8 9
1
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)的更多相关文章
- 有意思的DP(CF360B Levko and Array)
刚才面试了一个蛮有意思的DP题目,脑子断片,没写出来,不过早上状态还是蛮好的 一个长度为n的序列最多改变k次,使相邻两数之差绝对值的最大值最小 三维的dp我先尝试写一下 Codeforces 360B ...
- 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. ...
- 33. Search in Rotated Sorted Array(二分查找)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- Codeforces 361D Levko and Array(二分)(DP)
Levko and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- hdu2993之斜率dp+二分查找
MAX Average Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- (二分查找 结构体) leetcode33. Search in Rotated Sorted Array
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- (二分查找 拓展) 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 ...
- leetcode 二分查找 Search in Rotated Sorted Array
Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions Suppose ...
- 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 ...
随机推荐
- 嵌入式linux驱动开发之给linux系统添加温度传感器模块
忙了几天,终于可以让ds18b20在自己的开发板的linux系统上跑了!虽然ds18b20不是什么新鲜玩意,但是想想知己可以给linux系统添加模块了还是有点小鸡冻呢! 虽然说现在硬件的资源非常丰富而 ...
- $self $index $first $last parent() outerParent()
index5.html <html><head> <title>$self $index $first $last parent() outerParent()&l ...
- dom4j 使用总结
dom4j是一个Java的XML API,类似于jdom,用来读写XML文件 dom4j的使用方法简单总结来说如下: ①可以创建一个新的xml文件 ②利用SAXReader和File对象创建一个已存在 ...
- Unity3D独立游戏开发日记(二):摆放建筑物
在沙盒游戏里,能自由建造是很重要的特点,比如说风靡全球的<我的世界>,用一个个方块就能搭建出规模宏大的世界.甚至有偏激的人说,没有自由建造,就不是一个真正的沙盒游戏.的确,沙盒游戏的魅力有 ...
- 第十二章:window对象
第十一章介绍了window对象及其客户端javascript所扮演的核心角色:它是客户端javascript程序的全局对象.本章介绍window对象的属性和方法,这些属性定义了不同的API,但是只有一 ...
- SQL删除重复的记录(只保留一条)
首先新建表: --创建示例表 CREATE TABLE t ( id ,) PRIMARY KEY, a ), b ) ) --插入数据 INSERT INTO t SELECT 'aa','bb' ...
- Moqui学习之数据与资源
资源位置: 资源门面位置的字符串类似于URL的构成方式:协议,主机,可选端口和文件名.它支持标准的java URL协议(http https ftp jar file).同样也支持一些扩展的协议: c ...
- 20步打造最安全的NGINX WEB服务器
Nginx 是一个轻量级的,高性能的Web服务器以及反向代理和邮箱(IMAP/POP3)代理服务器.它运行在UNIX,GNU /linux,BSD 各种版本,Mac OS X,Solaris和Wind ...
- 如何在Dreamweaver中使用emmet
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=3666 一.emmet ...
- jQuery 文本编辑器插件 HtmlBox 使用
0.htmlbox下载地址:http://download.csdn.net/detail/leixiaohua1020/6376479 1.引入头文件 <script src="li ...