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.
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 ... 
随机推荐
- Java系列:Add Microsoft SQL JDBC driver to Maven
			Maven does not directly support some libraries, like Microsoft's SQL Server JDBC. This tutorial will ... 
- C语言学习的记忆
			优于他人的技能 会玩双截棍: 我的经验就是Practice make perfect,熟能生巧:还有就是坚持不懈. 关于C语言的学习的回忆 1.我通过老师的教导和课外C语言书籍中学习,和我的技能相比, ... 
- 20145222黄亚奇《Java程序设计》第8周学习总结
			教材学习内容总结 第15章 通用API 1 日志 1 日志API简介 java.util.loggging包提供了日志功能相关类与接口. 使用日志的起点是Logger类,Logger类的构造函数表示为 ... 
- Jenkins进阶之自动发送邮件的Default Content设置模板
			分享一个简洁实用的Jenkins项目邮件管理系统的"Default Content"设置模板 配置如下: <h1><center><font colo ... 
- 推荐一款超强大的基于Angularjs的自动完成(Autocomplete)标签及标签组插件–ngTagsInput
			前言 今天利用中午午休时间,给大家分享推荐一款基于Angularjs的自动完成(Autocomplete)标签及标签组插件--ngTagsInput,功能超强大的.不信,你试试就知道^_^... Au ... 
- 编写高质量代码--改善python程序的建议(八)
			原文发表在我的博客主页,转载请注明出处! 建议四十一:一般情况下使用ElementTree解析XML python中解析XML文件最广为人知的两个模块是xml.dom.minidom和xml.sax, ... 
- [C++基础]一个比较常用的配置文件/初始化文件读取程序
			在编程中,我们经常会遇到一些配置文件或初始化文件.这些文件通常后缀名为.ini或者.conf,可以直接用记事本打开.里面会存储一些程序参数,在程序中直接读取使用.例如,计算机与服务器通信,服务器的ip ... 
- css中的伪类和伪元素
			伪类用单冒号 我们平时熟悉的a:link.a:visited.a:hover和a : active 伪元素用双冒号(为了更好的兼容我们也用单冒号) 常用的:before :after和 :fir ... 
- c# TextBox只允许输入数字,禁用右键粘贴,允许Ctrl+v粘贴数字
			TextBox只允许输入数字,最大长度为10 //TextBox.ShortcutsEnabled为false 禁止右键和Ctrl+v private void txtNumber_KeyPress( ... 
- Daily Scrum – 1/7
			Meeting Minutes 搞定了一个bug,单词面板滚动条的bug: 在电脑屏幕上的屏幕适配有了新思路: Progress part 组员 今日工作 Time (h) 明日计划 Time ( ... 
