-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathsecvdist.in.cpp
40 lines (35 loc) · 883 Bytes
/
secvdist.in.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# include <fstream>
# include <algorithm>
# include <vector>
# include <deque>
# define NR 1000005
using namespace std;
ifstream f("secvdist.in");
ofstream g("secvdist.out");
deque <int> dmin, dmax;
int i,j,n,m,xi,ci,K,L;
int a[NR];
int main ()
{
f>>n>>K;
ci=1;
for (i=1; i<=n; ++i) {
f>>a[i];
//punem in dmin
while (! dmin.empty() && a[dmin.front()] >= a[i])
dmin.pop_back();
dmin.push_back(i);
//punem in dmax
while (! dmax.empty() && a[dmax.front()] <= a[i])
dmax.pop_back();
dmax.push_back(i);
while (! dmin.empty() && ! dmax.empty() && (a[dmax.front()] - a[dmin.front()])>K) {
if (dmin.front()==ci) dmin.pop_front();
if (dmax.front()==ci) dmax.pop_front();
++ci;
}
L=max(L, i-ci+1);
}
g<<L<<"\n";
return 0;
}