Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string>
- #include <cstring>
- #include <utility>
- #include <iostream>
- #include <iomanip>
- #include <vector>
- #include <algorithm>
- #include <numeric>
- #include <cassert>
- #include <stack>
- using namespace std;
- const int INF = 1000000010;
- struct Info
- {
- int x, minv, maxv;
- };
- stack<Info> s1, s2;
- int getMin()
- {
- if(s1.empty())
- return s2.top().minv;
- else if(s2.empty())
- return s1.top().minv;
- else
- return min(s1.top().minv, s2.top().minv);
- }
- int getMax()
- {
- if(s1.empty())
- return s2.top().maxv;
- else if(s2.empty())
- return s1.top().maxv;
- else
- return max(s1.top().maxv, s2.top().maxv);
- }
- void stack_push(stack<Info>& st, int x)
- {
- if(st.empty())
- st.push(Info{x,x,x});
- else
- st.push(Info{x, min(st.top().minv, x), max(st.top().maxv, x)});
- }
- void push(int x)
- {
- stack_push(s1, x);
- }
- void pop()
- {
- if ( s2.empty() )
- {
- while(!s1.empty())
- {
- stack_push(s2, s1.top().x);
- s1.pop();
- }
- }
- s2.pop();
- }
- int main()
- {
- int n,k;
- scanf("%d%d", &n, &k);
- vector<int> a(n);
- for(int i=0;i<n;i++)
- scanf("%d", &a[i]);
- int l;
- int res = 0;
- for(int r = 0; r < n ; r++)
- {
- push(a[r]);
- while(getMax()-getMin()>k)
- {
- pop();
- l++;
- }
- res=max(res, r-l+1);
- }
- printf("%d\n", res);
- }
Advertisement
Add Comment
Please, Sign In to add comment