Pagini recente » Cod sursa (job #999140) | Cod sursa (job #995301) | Cod sursa (job #155977) | Cod sursa (job #839430) | Cod sursa (job #2067713)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("secvdist.in");
ofstream out("secvdist.out");
const int N=1000001;
int v[N],dmax[N],dmin[N];
int main()
{
int j,sc,lmax,stmax,drmax,k,n,stmin,drmin,i;
stmax=0;
stmin=0;
drmax=-1;
drmin=-1;
in>>n>>k;
j=0;
sc=0;
for(i=0;i<n;i++)
in>>v[i];
for(i=0; i<n; i++)
{
while(stmax<=drmax && v[i]>=v[dmax[drmax]])
{
drmax--;
}
dmax[++drmax]=i;
while(stmin<=drmin && v[i]<=v[dmin[drmin]])
{
drmin--;
}
dmin[++drmin]=i;
while(j<=i && v[dmax[stmax]]-v[dmin[stmin]]>k)
{
if(dmax[stmax]==j)
{
stmax++;
}
if(dmin[stmin]==j)
{
stmin++;
}
j++;
}
//out << j << " " << i << "\n";
lmax=max(lmax,i-j+1);
}
out<<lmax;
return 0;
}