Guest User

Untitled

a guest
Jan 3rd, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define ans abs
  3. using namespace std;
  4. int tab[3*(int)1e6+5];
  5. int main(){
  6. ios_base::sync_with_stdio(0);
  7. cin.tie(0);
  8. cout.tie(0);
  9. int od,n;
  10. cin>>od>>n;
  11. for(int i=0; i<n; i++){
  12. cin>>tab[i];
  13. }
  14. set<pair<int,int>>wychylenia;
  15. wychylenia.insert(make_pair(tab[0],0));
  16. int dlugosc=1;
  17. int p=0;
  18. int k=0;
  19. int maks=1;
  20. while(1){
  21. maks=max(maks,dlugosc);
  22. int a=(*wychylenia.begin()).first;
  23. int b=(*--wychylenia.end()).first;
  24. if(k!=n-1&&ans(tab[k+1]-a)<=od&&ans(tab[k+1]-b)<=od){
  25. //cout<<"dodawane "<<tab[k+1]<<"\n";
  26. k++;
  27. wychylenia.insert(make_pair(tab[k],k));
  28. dlugosc++;
  29. maks=max(maks,dlugosc);
  30. }
  31. else if(p==k&&p!=n-1){
  32. p++;
  33. k++;
  34. //cout<<"przesuniecie "<<tab[p]<<"\n";
  35. wychylenia.insert(make_pair(tab[k],k));
  36. dlugosc=1;
  37. maks=max(maks,dlugosc);
  38. }
  39. else if(p<k){
  40. //cout<<"przesuwam p usuwam "<<tab[p]<<"\n";
  41. wychylenia.erase(make_pair(tab[p],p));
  42. p++;
  43. dlugosc--;
  44. maks=max(maks,dlugosc);
  45. }
  46. else break;
  47. }
  48. cout<<maks;
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment