这个题纯净水啊……
开两个单调队列,然后用滑动窗口法即可。
/**************************************************************
Problem: 2096
User: danihao123
Language: C++
Result: Accepted
Time:5656 ms
Memory:24640 kb
****************************************************************/
#include <cstdio>
#include <cctype>
#include <queue>
#include <deque>
#include <algorithm>
using namespace std;
const int maxn=3000005;
namespace DDQueue{
queue<int> Q;
deque<int> Min,Max;
inline void push(int x){
Q.push(x);
while(!Min.empty() && Min.back()>x)
Min.pop_back();
Min.push_back(x);
while(!Max.empty() && Max.back()<x)
Max.pop_back();
Max.push_back(x);
}
inline void pop(){
if(Q.empty())
return;
if(Q.front()==Min.front())
Min.pop_front();
if(Q.front()==Max.front())
Max.pop_front();
Q.pop();
}
inline int get_ans(){
return Max.front()-Min.front();
}
inline int size(){
return Q.size();
}
};
inline char GetC(){
char c;
fread(&c,1,1,stdin);
return c;
}
inline int readint(){
register int x=0;
register char c=GetC();
while(!isdigit(c))
c=GetC();
while(isdigit(c)){
x=x*10+c-'0';
c=GetC();
}
return x;
}
int A[maxn];
int main(){
register int k,n;
register int i,ans=0;
k=readint();
n=readint();
for(i=1;i<=n;i++){
A[i]=readint();
}
for(i=1;i<=n;i++){
DDQueue::push(A[i]);
while(DDQueue::get_ans()>k)
DDQueue::pop();
ans=max(ans,DDQueue::size());
}
printf("%d\n",ans);
return 0;
}