博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #333 (Div. 2) B. Approximating a Constant Range
阅读量:7008 次
发布时间:2019-06-28

本文共 2472 字,大约阅读时间需要 8 分钟。

B. Approximating a Constant Range
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 test(s)
input
51 2 3 3 2
output
4
input
115 4 5 5 6 7 8 8 8 7 6
output
5
Note

In the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.

In the second sample, there are three almost constant ranges of length 4[1, 4][6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10].

题目大意:

给你一串数,让你求最大的区间(最大值-最小值<=1)

解题思路:

我是用集合做的。。。

#include 
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define MM(a) memset(a,0,sizeof(a))typedef long long LL;typedef unsigned long long ULL;const int maxn = 1e5+5;const int mod = 1e9+7;const double eps = 1e-10;const int INF = 0x3f3f3f3f;LL gcd(LL a, LL b){ if(b == 0) return a; return gcd(b, a%b);}multiset
s;int arr[maxn];int main(){ int n; while(cin>>n) { s.clear(); for(int i=0; i
1) { s.erase(s.find(arr[i])); i++; } ret = max(ret, int(s.size())); j++; } cout<
<

转载地址:http://jfitl.baihongyu.com/

你可能感兴趣的文章
学习笔记-实验楼项目课(Linux桌面字典)
查看>>
Spring基础问答
查看>>
iOS8 相机拍照问题 Snapshotting a view
查看>>
comparable 接口的使用示例
查看>>
剑指Offer之重建二叉树(题6)
查看>>
strace-跟踪进程执行时的系统调用
查看>>
三个数找最大 1.0
查看>>
判断大端与小端
查看>>
计算机科学技术基础知识之多媒体知识
查看>>
如何禁用笔记本键盘
查看>>
第一次开博客,虽然有点晚了,用来鞭策激励一下自己,加油!
查看>>
4.4作业(变更管理+安全管理)
查看>>
(二)JSP语法详细介绍--指令元素
查看>>
python 数据库处理
查看>>
ORACLE 字符集问题
查看>>
我今天为了理想而开始了新的页面
查看>>
Linux Bash Shell日期格式化和计算
查看>>
微软:俄罗斯APT28间谍组织袭击欧洲政治机构
查看>>
企业如何克服混合云存储问题
查看>>
mysql和oracle用法的区别
查看>>