cf 602 B(模拟)
来源:互联网 发布:网络直播公司创业计划 编辑:程序博客网 时间:2024/06/12 01:00
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.
51 2 3 3 2
4
115 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].
#include <bits/stdc++.h>using namespace std;multiset <int> s;int a[100005];int main(){ int n,ans=0; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",a+i); int j=0,i=0; while(j<n) { s.insert(a[j]); if(abs(*s.begin()-*s.rbegin())>1) { s.erase(s.find(a[i])); i++; } ans=max(ans,int(s.size())); j++; } printf("%d\n",ans); return 0;}
- cf 602 B(模拟)
- cf 697 B(模拟)
- CF 9B模拟
- CF 342B(Xenia and Spies-模拟)
- CF#274 (Div. 2) B.(暴力模拟)
- 某场CF B Octagons(模拟)
- cf 583 B. Robot's Task(模拟)
- cf#344-B - Print Check-暴力模拟
- cf 758 B. Blown Garland模拟
- cf -148 b double追赶问题模拟
- CF 868 B. Race Against Time【模拟】
- [CF 239B][BNUOJ 26625] King's Path [模拟]
- CF#310-B - Case of Fake Numbers-模拟水题
- CF#322-B. Luxurious Houses-模拟水题
- cf#323-div2-B. Robot's Task-简单模拟
- CF#305-B. Mike and Fun-暴力模拟水题
- cf#339 -B. Gena's Code -模拟乘法
- CF 501B Misha and Changing Handles 模拟,水题
- ST3插件——PlainTasks的使用
- Xshell本地指令大全
- Libgdx 之Group视图裁剪
- Android常见的Event-driven方案
- Redis 学习笔记3-排序
- cf 602 B(模拟)
- Eclipse将引用了第三方jar包的Java项目打包成jar
- 数据库设计-初认识
- 黑马程序员——IO流知识详解
- spotlight禁用与恢复
- Android Studio来了,它能取代Eclipse吗?
- memset用法详解(转)
- cf 602 C(最短路)
- ZooKeeper运维之使用SnapshotFormatter可视化快照数据