Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define ans abs
- using namespace std;
- int tab[3*(int)1e6+5];
- int main(){
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int od,n;
- cin>>od>>n;
- for(int i=0; i<n; i++){
- cin>>tab[i];
- }
- set<pair<int,int>>wychylenia;
- wychylenia.insert(make_pair(tab[0],0));
- int dlugosc=1;
- int p=0;
- int k=0;
- int maks=1;
- while(1){
- maks=max(maks,dlugosc);
- int a=(*wychylenia.begin()).first;
- int b=(*--wychylenia.end()).first;
- if(k!=n-1&&ans(tab[k+1]-a)<=od&&ans(tab[k+1]-b)<=od){
- //cout<<"dodawane "<<tab[k+1]<<"\n";
- k++;
- wychylenia.insert(make_pair(tab[k],k));
- dlugosc++;
- maks=max(maks,dlugosc);
- }
- else if(p==k&&p!=n-1){
- p++;
- k++;
- //cout<<"przesuniecie "<<tab[p]<<"\n";
- wychylenia.insert(make_pair(tab[k],k));
- dlugosc=1;
- maks=max(maks,dlugosc);
- }
- else if(p<k){
- //cout<<"przesuwam p usuwam "<<tab[p]<<"\n";
- wychylenia.erase(make_pair(tab[p],p));
- p++;
- dlugosc--;
- maks=max(maks,dlugosc);
- }
- else break;
- }
- cout<<maks;
- }
Advertisement
Add Comment
Please, Sign In to add comment