【32.22%】【codeforces 602B】Approximating a Constant Range
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
Examples
input
5
1 2 3 3 2
output
4
input
11
5 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].
【题目链接】:http://codeforces.com/contest/602/problem/B
【题解】
用ST算法先搞出各个区间的最大、最小值
然后枚举区间的左端点、二分右端点.
判断依据就是最大值和最小值的差;
如果小于等于1则右端点可以再往右点。否则往左
一般用二分都能过吧。
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int MAXN = 1e5+100;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
int n;
int a[MAXN];
int ma[MAXN][18],mi[MAXN][20];
int pre2[20];
int need[MAXN];
int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);
rep1(i,1,n)
rei(a[i]),mi[i][0] = ma[i][0] = a[i];
pre2[0] = 1;
for (int i = 1; i <= 18; i++)
pre2[i] = pre2[i - 1] << 1;
need[1] = 0; need[2] = 1;
int temp = 2;
for (int i = 3; i <= n; i++)
if (pre2[temp] == i)
need[i] = need[i - 1] + 1, temp++;
else
need[i] = need[i - 1];
for (int l = 1; pre2[l] <= n; l++)
for (int i = 1;i <= n;i++)
if (i + pre2[l] - 1 <= n)
{
ma[i][l] = max(ma[i][l - 1], ma[i + pre2[l - 1]][l - 1]);
mi[i][l] = min(mi[i][l - 1], mi[i + pre2[l - 1]][l - 1]);
}
int ans = 0;
rep1(i,1,n)
{
int l = i,r = n,tempr=l;
while (l <= r)
{
int m = (l+r)>>1;
int le = need[m-i+1];
int mi1 = min(mi[i][le],mi[m-pre2[le]+1][le]);
int ma1 = max(ma[i][le],ma[m-pre2[le]+1][le]);
if (abs(mi1-ma1)<=1)
{
tempr = m;
l = m+1;
}
else
r = m-1;
}
if(tempr-i+1> ans)
ans = tempr-i+1;
}
cout << ans << endl;
return 0;
}
close
【32.22%】【codeforces 602B】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 ...
- codeforce -602B 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 st 二分
B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...
- 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 602B】G - 一般水的题2-Approximating a Constant Range
Description When Xellos was doing a practice course in university, he once had to measure the intens ...
- 【CodeForces 602C】H - Approximating a Constant Range(dijk)
Description through n) and m bidirectional railways. There is also an absurdly simple road network — ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- CF 602B Approximating a Constant Range
(●'◡'●) #include<iostream> #include<cstdio> #include<cmath> #include<algorithm& ...
- 【Selenium】【BugList9】windows环境,fp = open("./"+ time.strftime("%Y-%m-%d %H:%M:%S") + " result.html",'wb'),报错:OSError: [Errno 22] Invalid argument: './2018-09-05 10:29:32 result.html'
[代码] if __name__=="__main__": suite = unittest.TestSuite() suite.addTest(Baidu("test_ ...
随机推荐
- WPF通用框架 数据库结构
前言 由於技術轉型, 目前大部分工作都是WPF為主, 但是趨於如今想在網絡上找一套能夠滿意的WPF權限管理框架太難, 因為WinForm那時候是有一套改寫過的權限框架, 所以數據庫設計這塊已經有了一個 ...
- 各种join一目了然: join 、inner join、left join 、right join、full join
各种join一幅图一目了然 一下每幅图都是指: A * join B on A.id = B.in 这个帖子也非常形象.比較好:http://www.phpddt.com/db/inner_join- ...
- Android 多种方式正确的载入图像,有效避免oom
图像载入的方式: Android开发中消耗内存较多一般都是在图像上面.本文就主要介绍如何正确的展现图像降低对内存的开销,有效的避免oom现象. 首先我们知道我的获取图像的来源一般有三种源 ...
- ZOJ QS Network
QS Network Time Limit: 2 Seconds Memory Limit: 65536 KB Sunny Cup 2003 - Preliminary Round Apri ...
- 【agc009b】Tournament
Description 一场锦标赛有n个人,总共举办n-1次比赛,每次比赛必定一赢一输,输者不能再参赛.也就是整个锦标赛呈一个二叉树形式.已知一号选手是最后的胜者,以及对于i号选手(i>1)都知 ...
- Javascript 继承和克隆
个人总结: call 继承的是父类私有 prototype 继承的父类公有 create 可以将公有或私有继承到子类上去(克隆) for in 克隆 不管公有还是私有的都克隆成私有的 1.原型继承:将 ...
- echarts如何设置背景图的颜色
公司的业务涉及到统计图的有很多,最近一直echarts里面踩各种坑,感觉应该建立一个echarts专题才对,前端的东西博大精深,无论在哪一个知识点,只要细细深究,都是别有一方天地在等待,随着需求的不同 ...
- 优秀Java程序员必备10招
1. 拥有扎实的基础和深刻理解 OO 原则 对于 Java 程序员,深刻理解 ObjectOriented Programming(面向对象编程)这一概念是必须的.没有 OOPS 的坚实基础,就领会不 ...
- git还原文件
http://jingyan.baidu.com/album/e4511cf33479812b855eaf67.html
- android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动
android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动 1.先简单设置一个闹钟提醒事件: //设置闹钟 mSetting.setOnClickListener ...