fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. #include <vector>
  5. #include <bitset>
  6. #include <stdio.h>
  7. #include <math.h>
  8. using namespace std;
  9. typedef std::vector<int> vi;
  10. typedef std::vector<pair<int, int> > vii;
  11. #define FOR(l) for(vi::iterator it=l.begin();it!=l.end();it++)
  12. #define FOR_L(l, s, e) for(vi::iterator it=l.begin()+s;it!=l.end()-e;it++)
  13. inline int input(){
  14. int t=0;
  15. char c;
  16. c=getchar ();
  17. while(c<'0' || c>'9')
  18. c=getchar ();
  19. while(c>='0' && c<='9')
  20. {
  21. t=(t<<3)+(t<<1)+c-'0';
  22. c=getchar ();
  23. }
  24. return(t);
  25. }
  26.  
  27. //----------Main source code -----------------//
  28. int p[100000 +1], P;
  29. int main() {
  30. int y, i, first, last, mid, end, ans=0, af, al;
  31. while(scanf("%d",&y)!=EOF){
  32. P=input();
  33. ans=0;
  34. for(i=0;i<P;i++)
  35. p[i]=input();
  36. for(i=0;i<P;i++){
  37. //search upper_bound by binary search
  38. first = i+1;
  39. last = P-1;
  40. end = p[i]+y-1;
  41. while(last>first){
  42. mid = (first+last)/2;
  43. if(p[mid]>end) last=mid;
  44. else first = mid+1;
  45. }
  46. if(ans<last-i){
  47. ans=last-i;
  48. af=p[i];
  49. al=p[last-1];
  50. }
  51. }
  52. printf("%d %d %d\n", ans, af, al);
  53. }
  54. return 0;
  55. }
Not running #stdin #stdout 0s 0KB
stdin
5
20
1
2
3
6
8
12
13
13
15
16
17
18
19
20
20
21
25
26
30
31
stdout
Standard output is empty