Codeforces Round #333 (Div. 2) B
2 seconds
256 megabytes
standard input
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.
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.
5
1 2 3 3 2
4
11
5 4 5 5 6 7 8 8 8 7 6
5
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
题解:
例如
5
1 2 3 3 2
差值分别为 2-1=1;
3-2=1;
3-3=0;
2-3=-1; 另外 it's guaranteed that |ai + 1 - ai| ≤ 1.
可以判断 当连续的差值或相隔差值为0 的两个差值 相等时 该段区间结束 更新最大值
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,a;
int main()
{
scanf("%d",&n);
scanf("%d",&a);
int cha=0;
int judge=0;
int l=0,r=0;
int ans=0,exm;
for(int i=1; i<n; i++)
{
scanf("%d",&exm);
cha=exm-a;//计算差值
a=exm;
if(cha==0)//差值为零 相等时
continue;
if(cha!=judge)//当前差值与 之前一个差值比较
{
judge=cha;//更新到当前区间
r=i;
}
else
{
if(i-l>ans)//更新区间大小
ans=i-l;
l=r;
r=i;
}
}
if(n-l>ans)//特列 后端 都相等
ans=n-l;
cout<<ans<<endl;
return 0;
}
Codeforces Round #333 (Div. 2) B的更多相关文章
- 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) 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 水题
A. Two Bases Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/602/problem/ ...
- 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. 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. 2)
水 A - Two Bases 水题,但是pow的精度不高,应该是转换成long long精度丢失了干脆直接double就可以了.被hack掉了.用long long能存的下 #include < ...
- Codeforces Round #333 (Div. 1)--B. Lipshitz Sequence 单调栈
题意:n个点, 坐标已知,其中横坐标为为1~n. 求区间[l, r] 的所有子区间内斜率最大值的和. 首先要知道,[l, r]区间内最大的斜率必然是相邻的两个点构成的. 然后问题就变成了求区间[l, ...
随机推荐
- go通过第三方库 mahonia gbk 转utf8
go get github.com/axgle/mahonia dec := mahonia.NewDecoder("GBK")ret:=dec.ConvertString(res ...
- php导出excel表格的使用
网站后台有很多列表数据,常常都会有导出excel表格的需求,和大家分享一个实用的导出excel表格方法: 不多说,上代码: /** * @param array $data 要导出的数据 * @par ...
- lintcode204 单例
单例 单例 是最为最常见的设计模式之一.对于任何时刻,如果某个类只存在且最多存在一个具体的实例,那么我们称这种设计模式为单例.例如,对于 class Mouse (不是动物的mouse哦),我们应 ...
- python常用命令—windows终端查看安装包信息
1, pip list 会将 Python 的所有安装包全部显示出来, 左边是包名, 右边是包的版本号. 2, pip show 包的名字 会将这个包的名字,版本号,包的功能说明,按装这个包的路径显示 ...
- 蓝桥杯算法训练 区间k大数查询
算法训练 区间k大数查询 问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示序列长度. 第二行包含n个正整数,表示给定的序列. 第三个 ...
- java面试整理
IO和NIO的区别 这是一个很常见的问题,如果单纯的只回答IO和NIO的区别,只能算及格.我个人觉得应该从以下几个方面回答: 1).IO简介, 2).TCP的三次握手,因为这也是两者的区别之一, 3) ...
- java安装环境变量设置
1,依次打开:我的电脑-->属性-->高级-->环境变量 2,设置用户变量 新建 JAVA_HOME C:\Program Files\Java\j2sdk1.5.0 (JDK的安装 ...
- Java 变量和输入输出
一些重要知识 一个源文件里只能有一个public类,其它类数量不限.文件名与public类名相同 JAVA程序严格区分大小写 JAVA应用程序的执行入口是main方法固定写法:public stati ...
- c#笔记整理 关于继承与多态等
[ 塔 · 第 二 条 约 定 ] c#面向对象基础 整理private.protected.public.abstract等的异同 public 公有访问.不受任何限制. private 私有访问. ...
- Deeplearning——Logistics回归
资料来源:1.博客:http://binweber.top/2017/09/12/deep_learning_1/#more——转载,修改更新 2.文章:https://www.qcloud.com/ ...