Codeforces Round #333 (Div. 2) B. Approximating a Constant Range st 二分
B. Approximating a Constant Range
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/602/problem/B
Description
When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging — but why not make a similar programming contest problem while we're at it?
You're given a sequence of n data points a1, ..., an. There aren't any big jumps between consecutive data points — for each 1 ≤ i < n, it's guaranteed that |ai + 1 - ai| ≤ 1.
A range [l, r] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let M be the maximum and m the minimum value of ai for l ≤ i ≤ r; the range [l, r] is almost constant if M - m ≤ 1.
Find the length of the longest almost constant range.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of data points.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000).
Output
Print a single number — the maximum length of an almost constant range of the given sequence.
Sample Input
5
1 2 3 3 2
Sample Output
4
HINT
题意
给你n个数,要求你找到最长的区间,使得这个区间的最大值减去最小值之差的绝对值小于等于1
题解:
枚举每一个数,以这个数为这个区间的最小值,能够往左边延伸多少,往右边延伸多少
再枚举每一个数,以这个数为区间的最大值,能够往左边延伸多少,往右边延伸多少就好了
可以O(n) 也可以 像我一样 用倍增然后二分去找
代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define maxn 100005
int n;
int dp[maxn][];
int dp1[maxn][];
int a[maxn];
int L[maxn],R[maxn];
int mm[maxn];
void initrmp(int n)
{
mm[]=-;
for(int i=;i<=n;i++)
{
mm[i]=((i&(i-))==)?mm[i-]+:mm[i-];
dp[i][]=a[i];
dp1[i][]=a[i];
}
for(int j = ;j<=mm[n];j++)
for(int i=;i+(<<j)-<=n;i++)
dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
for(int j = ;j<=mm[n];j++)
for(int i=;i+(<<j)-<=n;i++)
dp1[i][j]=min(dp1[i][j-],dp1[i+(<<(j-))][j-]);
}
int queryMax(int l,int r)
{
int k = mm[r-l+];
return max(dp[l][k],dp[r-(<<k)+][k]);
}
int queryMin(int l,int r)
{
int k = mm[r-l+];
return min(dp1[l][k],dp1[r-(<<k)+][k]);
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
int Ans = ;
initrmp(n);
for(int i=;i<=n;i++)
{
int l=,r=i;
while(l<=r)
{
int mid = (l+r)/;
if(abs(a[i]-queryMin(mid,i))<= && abs(a[i]-queryMax(mid,i))<=)r=mid-;
else l=mid+;
}
L[i]=l;
l=i,r=n;
while(l<=r)
{
int mid = (l+r)/;
if(abs(a[i]-queryMin(i,mid))<= && abs(a[i]-queryMax(i,mid))<=)l=mid+;
else r=mid-;
}
R[i]=l-;
}
for(int i=;i<=n;i++)
Ans = max(Ans,R[i]-L[i]+);
for(int i=;i<=n;i++)
{
int l=,r=i;
while(l<=r)
{
int mid = (l+r)/;
if(abs(a[i]-queryMin(mid,i))<= && abs(a[i]-queryMax(mid,i))<=)r=mid-;
else l=mid+;
}
L[i]=l;
l=i,r=n;
while(l<=r)
{
int mid = (l+r)/;
if(abs(a[i]-queryMin(i,mid))<= && abs(a[i]-queryMax(i,mid))<=)l=mid+;
else r=mid-;
}
R[i]=l-;
}
for(int i=;i<=n;i++)
Ans = max(Ans,R[i]-L[i]+);
cout<<Ans<<endl;
}
Codeforces Round #333 (Div. 2) B. Approximating a Constant Range st 二分的更多相关文章
- Codeforces Round #333 (Div. 2) B. Approximating a Constant Range
B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...
- Codeforces Round #333 (Div. 2)
水 A - Two Bases 水题,但是pow的精度不高,应该是转换成long long精度丢失了干脆直接double就可以了.被hack掉了.用long long能存的下 #include < ...
- Codeforces Round #333 (Div. 2) B
B. Approximating a Constant Range time limit per test 2 seconds memory limit per test 256 megabytes ...
- Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp
C. Kleofáš and the n-thlon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- Codeforces Round #333 (Div. 1) B. Lipshitz Sequence 倍增 二分
B. Lipshitz Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/601/ ...
- Codeforces Round #333 (Div. 2) C. The Two Routes flyod
C. The Two Routes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/602/pro ...
- Codeforces Round #333 (Div. 2) A. Two Bases 水题
A. Two Bases Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/602/problem/ ...
- Codeforces Round #333 (Div. 1) D. Acyclic Organic Compounds trie树合并
D. Acyclic Organic Compounds You are given a tree T with n vertices (numbered 1 through n) and a l ...
- Codeforces Round #333 (Div. 1)--B. Lipshitz Sequence 单调栈
题意:n个点, 坐标已知,其中横坐标为为1~n. 求区间[l, r] 的所有子区间内斜率最大值的和. 首先要知道,[l, r]区间内最大的斜率必然是相邻的两个点构成的. 然后问题就变成了求区间[l, ...
随机推荐
- Squid 反向代理加速网站
本实例的域名是 wenjin.cache.ibm.com.cn,通过DNS的轮询 技术,将客户端的请求分发给其中一台 Squid 反向代理服务器处理,如果这台 Squid 缓存了用户的请求资源,则将请 ...
- 《C++ Primer 4th》读书笔记 第10章-关联容器
原创文章,转载请注明出处:http://www.cnblogs.com/DayByDay/p/3936464.html
- 用defy来潜水最终还是挂了........
defy526是6级,,不过好像这次我用来潜过去不足2米还是挂掉了... 国际通用的防水级别认证体系: IPX-0 没有防水保护 IPX-1 设备在正常操作状态下,可以提供相当于3-5毫米/分钟降雨的 ...
- [转] C#实现自动化Log日志
qing2005原文地址 C#实现自动化Log日志 在开发项目的时候,我们不免要使用Log记录日志,使用最多的是Log4Net和EntLib Log,在需要记录日志的代码处加入log.Write(日志 ...
- [Everyday Mathematics]20150116
设 $\al_n\geq 0$ 且 $\dps{\vlm{n}\al_n=0}$, 试求 $$\bex \vlm{n}\frac{1}{n}\sum_{k=1}^n \ln\sex{\frac{k}{ ...
- U盘安装Centos5.3
一.制作 U 盘启动引导盘 1. 插上 U 盘,打开 UltraISO 软件,打开CentOS-5.3-i386-bin-DVD.iso 文件: 2.点启动--写入硬盘镜像,在硬盘驱动器里面选择你的 ...
- GRID控件删除之前确认
<asp:TemplateField HeaderText="删除新闻" ShowHeader="False"><ItemTemplate&g ...
- SQL你必须知道的-增删改查与约束
SQL你必须知道的-增删改查与约束 -- 插入数据 --Insert 语句可以省略表名后的列名,但是不推荐 insert into Class values ('' 高一一班 '', ...
- AAC 格式分析
一直在做一个语音项目,到了测试阶段,近来不是很忙,想把之前做的内容整理一下. 关于AAC音频格式基本情况,可参考维基百科http://en.wikipedia.org/wiki/Advanced_Au ...
- Tkinter教程之Canvas篇(1)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811803 '''Tkinter教程之Canvas篇(1)'''# 提供可以用来进行绘图的Co ...