fork download
  1. /*
  2.   Cred : SunnyYeahBoi
  3.   It's my last chance (⌐■_■)
  4.   Problem :
  5. */
  6.  
  7. #include<bits/stdc++.h>
  8.  
  9. using namespace std;
  10.  
  11. #define int long long
  12. #define double long double
  13. #define endl "\n"
  14. #define NAME "a"
  15.  
  16. const int MAXN = 1e6 + 5;
  17. const int inf = 1e18;
  18. const int MOD = 1e9 + 7;
  19.  
  20. void FileInput(){
  21. if(fopen(NAME".inp" , "r") == NULL)
  22. freopen(NAME".inp" , "w" , stdout);
  23. freopen(NAME".inp" , "r" , stdin);
  24. freopen(NAME".out" , "w" , stdout);
  25. }
  26.  
  27. int n , k;
  28. int a[MAXN];
  29. int f[MAXN] , pref[MAXN];
  30.  
  31. void solve(){
  32. cin >> n >> k;
  33. for(int i = 1 ; i <= n ; i++)
  34. cin >> a[i];
  35.  
  36. sort(a + 1 , a + 1 + n);
  37.  
  38. for(int i = 1 ; i <= n ; i++){
  39. // tìm j nhỏ nhất sao cho a[j] >= a[i] - k
  40. int j = lower_bound(a + 1 , a + 1 + n , a[i] - k) - a;
  41. f[i] = (i - j + 1);
  42. pref[i] = max(pref[i - 1] , f[i]);
  43. }
  44.  
  45. int res = 0;
  46. for(int i = 1 ; i <= n ; i++){
  47. int j = lower_bound(a + 1 , a + 1 + n , a[i] - k) - a;
  48. res = max(res , (i - j + 1) + pref[j - 1]);
  49. }
  50. cout << res << endl;
  51. }
  52.  
  53. int32_t main(){
  54. FileInput();
  55. ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  56. int t = 1;
  57. // cin >> t;
  58. while(t--)
  59. solve();
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty