Advertisement
LEGEND2004

D

Feb 17th, 2024
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. #define F first
  6. #define S second
  7.  
  8. signed main()
  9. {
  10.  
  11.     int n , k;
  12.     cin >> n >> k;
  13.     int a[n + 5];
  14.     for(int i = 0; i < n; i++){
  15.         cin >> a[i];
  16.     }
  17.     int l = 0;
  18.     multiset<int> ms;
  19.     int ans = 0;
  20.     for(int r = 0; r < n; r++){
  21.         ms.insert(a[r]);
  22.         while(*ms.rbegin() - *ms.begin() > k){
  23.             ms.erase(ms.find(a[l]));
  24.             l++;
  25.         }
  26.         ans += (r - l + 1);
  27.     }
  28.     cout << ans << '\n';
  29. }
  30.  
  31.  
  32. /*
  33.  
  34. ms.erase(x); // butun x lari silir
  35. ms.erase(ms.find(x)) // bir x silir
  36.  
  37. 3  1  2  6  4
  38.  
  39. 1  2  3  1  2
  40.  
  41. 1 2 3 3 6
  42. */
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement