FZU 2016 summer train I. Approximating a Constant Range 单调队列
题目链接:
题目
I. Approximating a Constant Range
time limit per test:2 seconds
memory limit per test:256 megabytes
问题描述
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.
输入
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).
输出
Print a single number — the maximum length of an almost constant range of the given sequence.
样例
input
5
1 2 3 3 2
output
4
input
11
5 4 5 5 6 7 8 8 8 7 6
output
5
题意
求最大值最小值相差小于2的最大区间长度
题解
这一题由于相邻的数据最多差一,可以直接做,但是这里贴一个单调队列的模板吧,毕竟更通用,
开两个单调队列来维护窗口最大最小值。(这里的实现是有先队列+时间戳)下标其实就相当于时间戳了,对于窗口l,r,小于l的都是过期的。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define X first
#define Y second
#define mp make_pair
using namespace std;
typedef __int64 LL;
const int maxn=1e5+10;
int arr[maxn];
priority_queue<pair<int,int> > mi,ma;
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%d",&arr[i]);
int l=0,r=0;
int ans=-1;
while(r<n){
mi.push(mp(-arr[r],r));
ma.push(mp(arr[r],r));
while(-mi.top().X<ma.top().X-1){
l++;
while(mi.top().Y<l) mi.pop();
while(ma.top().Y<l) ma.pop();
}
ans=max(ans,r-l+1);
r++;
}
printf("%d\n",ans);
return 0;
}
FZU 2016 summer train I. Approximating a Constant Range 单调队列的更多相关文章
- Codeforces 602B Approximating a Constant Range(想法题)
B. Approximating a Constant Range When Xellos was doing a practice course in university, he once had ...
- 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 ...
- cf602B Approximating a Constant Range
B. Approximating a Constant Range time limit per test 2 seconds memory limit per test 256 megabytes ...
- 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 ...
- codeforce -602B Approximating a Constant Range(暴力)
B. Approximating a Constant Range time limit per test 2 seconds memory limit per test 256 megabytes ...
- 【32.22%】【codeforces 602B】Approximating a Constant Range
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【CodeForces 602C】H - Approximating a Constant Range(dijk)
Description through n) and m bidirectional railways. There is also an absurdly simple road network — ...
- CF 602B Approximating a Constant Range
(●'◡'●) #include<iostream> #include<cstdio> #include<cmath> #include<algorithm& ...
- #333 Div2 Problem B Approximating a Constant Range(尺取法)
题目:http://codeforces.com/contest/602/problem/B 题意 :给出一个含有 n 个数的区间,要求找出一个最大的连续子区间使得这个子区间的最大值和最小值的差值不超 ...
随机推荐
- 【转载】分析商品日均销量(DMS)对促销商品选择的意义
江苏省常州市信特超市有限公司副总经理高晓颖 随着中国零售业的进一步的开放,竞争日趋激烈,促销活动在日常经营中已经成为不可缺少的一部分,频繁的促销活动的开展,零售业经营管理者越来越觉得促销商品的选择难度 ...
- jQuery简单邮箱验证
function chekmail() { var szReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; ...
- 我的一点关于把WndProc指向类的成员函数的看法
转载请注明来源:http://www.cnblogs.com/xuesongshu/ 我以前经常考虑使用WNDCLASS.CreateThread之类的Windows API时如何在类里面调用,传入函 ...
- linux中重置服务器的mysql用户密码
本文章前提条件是自己经把mysql登录密码给忘记了,这个时间我们解决方法有很多,重新安装mysql数据库一切重来,另一种是通过下面文章重新设置root密码,下面我们一起来看看方法二吧. 最 近 ...
- QtPropertyBrowser+vs2010的安装与配置(转)
这一篇文章有些问题,后又写了一篇,地址是http://www.cnblogs.com/aminxu/p/4552410.html 转自http://blog.csdn.net/jingwenlai_s ...
- jquery的延迟加载插件Lazy Load Plugin for jQuery
下载:https://github.com/tuupola/jquery_lazyload 使用:http://www.appelsiini.net/projects/lazyload 翻译:http ...
- nyoj71--独木舟上的旅行
描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别.一条独木舟最多只能乘坐两个人,且乘客的总重量不能超过独木舟的最大承载量.我们要尽量减少这次活动中的花销,所以要找出可以安置所有旅 ...
- 第一章 C++简介
第一章 C++简介 1.1 C++特点 C++融合了3种不同的编程方式:C语言代表的过程性语言,C++在C语言基础上添加的类代表的面向对象语言,C++模板支持的泛型编程. 1.2 C语言及其编程 ...
- 增量与位置PID
转载:http://blog.sina.com.cn/s/blog_408540af0100b17n.html http://bbs.ednchina.com/BLOG_ARTICLE_211739. ...
- 转 XMLHttpRequest().readyState的五种状态详解
转 http://javathinker.blog.ccidnet.com/blog-htm-itemid-1262479-do-showone-type-blog-uid-36384.html 在& ...