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/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
题解:
我是先做到它的升级版:此题升级版
我大概讲一下吧:
不难发现:如果(l,r),那么(l+1,r)必然也合法。
所以我们枚举左端点,右端点是不断递增的,所以是线性的。
可以用ST表做,也可以用优先队列做。
优先队列代码:
#include<bits/stdc++.h>
using namespace std;
#define N 100050
int n,val[N],flag[N];
struct Node
{
int id,val;
bool operator <(const Node&b)const
{return val<b.val;}
};
template<typename T>void read(T&x)
{
int k=; char c=getchar();
x=;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit();
while(isdigit(c))x=x*+c-'',c=getchar();
x=k?-x:x;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
read(n);
for(int i=;i<=n;i++)read(val[i]);
int r=,ans=;
priority_queue<Node>mx,mi;
for(int i=;i<=n;i++)
{
while(r+<=n&&(mi.empty()||
(fabs(val[r+]-mx.top().val)<=&&fabs(val[r+]+mi.top().val)<=)))
{
mx.push(Node{r+,val[r+]});
mi.push(Node{r+,-val[r+]});
r++;
}
ans=max(ans,r-i+);
flag[i]=;
while(!mi.empty()&&flag[mi.top().id])mi.pop();
while(!mx.empty()&&flag[mx.top().id])mx.pop();
}
printf("%d\n",ans);
}
ST表代码
#include<bits/stdc++.h>
using namespace std;
#define N 100050
int n,val[N];
int mm[N],mi[N][],mx[N][];
template<typename T>void read(T&x)
{
int k=; char c=getchar();
x=;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit();
while(isdigit(c))x=x*+c-'',c=getchar();
x=k?-x:x;
}
void init_ST(int n)
{
mm[]=-;
for(int i=;i<=n;i++)
{
mm[i]=(i&(i-))==?mm[i-]+:mm[i-];
mx[i][]=mi[i][]=val[i];
}
for(int i=;i<=;i++)
for(int j=;j+(<<i)-<=n;j++)
{
mi[j][i]=min(mi[j][i-],mi[j+(<<(i-))][i-]);
mx[j][i]=max(mx[j][i-],mx[j+(<<(i-))][i-]);
}
}
int rmq(int x,int y)
{
int k=mm[y-x+];
int ans=max(mx[x][k],mx[y-(<<k)+][k]);
ans-=min(mi[x][k],mi[y-(<<k)+][k]);
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
read(n);
for(int i=;i<=n;i++)read(val[i]);
init_ST(n);
int r=,ans=;
for(int i=;i<=n;i++)
{
while(r+<=n&&rmq(i,r+)<=)r++;
ans=max(ans,r-i+);
}
printf("%d\n",ans);
}
Codeforces Round #333 (Div. 2) B. Approximating a Constant Range的更多相关文章
- 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 ...
- 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, ...
随机推荐
- FFT算法理解与c语言的实现
完整内容迁移至 http://www.face2ai.com/DIP-2-3-FFT算法理解与c语言的实现/ http://www.tony4ai.com/DIP-2-3-FFT算法理解与c语言的实现 ...
- [python之ipython] jupyter notebook在云端服务器上开启,本地访问
本地ssh到云端: ssh username@xxx.xxx.xxx.xxx -L127.0.0.1:7777:127.0.0.1:8888 把云端的8888端口映射到本地的7777端口 云端运行指令 ...
- xgboost 特征重要性计算
在XGBoost中提供了三种特征重要性的计算方法: ‘weight’ - the number of times a feature is used to split the data across ...
- Python数据抓取(1) —数据处理前的准备
(一)数据抓取概要 为什么要学会抓取网络数据? 对公司或对自己有价值的数据,80%都不在本地的数据库,它们都散落在广大的网络数据,这些数据通常都伴随着网页的形式呈现,这样的数据我们称为非结构化数据 如 ...
- JAVA编程思想第二章答案
欢迎访问我的CSDN博客查看https://mp.csdn.net/mdeditor/94797839# 有其他问题欢迎发送邮箱至hpzhangjunjiell@163.com 感谢
- 关于centos6版本执行程序报错:libc.so.6: version GLIBC_2.14 not found的解决
执行后程序报错: libc.so.6: version GLIBC_2.14 not found 这种情况是因为当前服务器glibc的版本比较低造成的(不出意外是glibc_2.12是最高版本): 1 ...
- Java-AQS源码详解(细节很多!)
ReentrantLock调用lock()时时序图: addWaiter方法: enq方法:自旋 它维护了一个volatile int state(代表共享资源)和一个FIFO线程等待队列(多线程争用 ...
- flutter中的生命周期函数
前言:生命周期是一个组件加载到卸载的整个周期,熟悉生命周期可以让我们在合适的时机做该做的事情,flutter中的State生命周期和android以及React Native的生命周期类似. 先看一张 ...
- No suitable constructor was found in NUnit Parameterised tests
No suitable constructor was found in NUnit Parameterised tests Fairly obvious, but can also happen i ...
- 自定义vue-cli生成项目模板配置(1)
最近在读<变量>,目前得到的认知之一:慢变量才是决定事物长期发展的因素. 打算自定义vue-cli的脚手架或者根据自己的需要设置项目模板的相关参数,很大程度与慢变量这个概念相关. 当然,我 ...