cf D. Levko and Array
http://codeforces.com/contest/361/problem/D
用二分搜索相邻两个数的差的绝对值,然后用dp记录数改变的次数。dp[i]表示在i之前改变的次数,如果|a[i]-a[j]|<=(i-j)*mid 需要改变。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL __int64
using namespace std;
const LL inf=;
int dp[];
int n,k;
LL a[];
LL ABS(LL a)
{
if(a<) return -a;
return a;
}
bool ok(LL c)
{
memset(dp,,sizeof(dp));
for(int i=; i<=n; i++)
{
dp[i]=i;
for(int j=; j<i; j++)
{
if(ABS(a[i]-a[j])<=(LL)(i-j)*c)
dp[i]=min(dp[i],dp[j]+(i-j-));
}
if(dp[i]+(n-i-)<=k) return true;
}
return false;
} int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
for(int i=; i<=n; i++)
{
scanf("%I64d",&a[i]);
}
LL l=,r=inf;
while(l<=r)
{
LL mid=(l+r)/;
if(ok(mid))
{
r=mid-;
}
else l=mid+;
}
printf("%I64d\n",l);
}
return ;
}
cf D. Levko and Array的更多相关文章
- cf C. Levko and Array Recovery
http://codeforces.com/contest/361/problem/C 这道题倒着一次,然后正着一次,在正着的一次的时候判断合不合法就可以. #include <cstdio&g ...
- [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 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 - 361D Levko and Array
Discription Levko has an array that consists of integers: a1, a2, ... , an. But he doesn’t like this ...
- CF 1008C Reorder the Array
You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it ...
- Codeforces Round #210 (Div. 2) C. Levko and Array Recovery
题目链接 线段树的逆过程,想了老一会,然后发现应该是包含区间对存在有影响,就不知怎么做了...然后尚大神,说,So easy,你要倒着来,然后再正着来,判断是不是合法就行了.然后我乱写了写,就过了.数 ...
- CF 295A Greg and Array (两次建树,区间更新,单点查询)
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...
随机推荐
- Android 查看是否有存储卡插入
String status=Environment.getExternalStorageState(); 2 3 if ( status.equals ( Enviroment.MEDIA_MOU ...
- ASP.NET MVC framework 学习
http://www.cnblogs.com/lmfeng/archive/2013/03/28/2986123.html MVC数据绑定方式 http://www.cnblogs.com/lmfe ...
- HAVING 子句 (SQL Server Compact)
MSDN官方文献 原文地址:http://technet.microsoft.com/zh-cn/library/ms173260.aspx
- zookeeper[5] zookeeper集群配置及伪集群配置
参考:http://zookeeper.apache.org/doc/trunk/zookeeperStarted.html 集群配置: 1.配置文件conf/zoo.cfg,除了单机模式的配置之外, ...
- CSS常用操作-对齐
index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...
- LaunchScreen.xib 设置启动图
1.LaunchScreen.xib 中拖入UIImageView 约束都设置为0 2.新建一个Image Set 起名为LaunchScreen Devices设置为:Device Speecif ...
- SSO跨域解决方案
单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.它包括可 以将 ...
- 10个利用Eclipse调试Java的常见技巧
http://www.open-open.com/news/view/1ad9099 阅读目录 1. Conditional Breakpoint 2. Exception Breakpoint 3. ...
- 关于MSHTML
本文翻译自http://msdn.microsoft.com/workshop/browser/mshtml/overview/overview.aspMSDN Home > MSDN Lib ...
- HDU 4119Isabella's Message2011成都现场赛I题(字符串模拟)
Isabella's Message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...