Advertisement
LEGEND2004

Junior D

Feb 9th, 2024
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. int cnt[105];
  6.  
  7. signed main()
  8. {
  9.     ios_base::sync_with_stdio(0);
  10.     cin.tie(0);
  11.  
  12.  
  13.     int n , k;
  14.     cin >> n >> k;
  15.     int a[n + 5];
  16.     for(int i = 0; i < n; i++){
  17.         cin >> a[i];
  18.     }
  19.     multiset<int> ms;
  20.     int l = 0 , ans = 0;
  21.     for(int i = 0; i < n; i++){
  22.         ms.insert(a[i]);
  23.         while(*ms.rbegin() - *ms.begin() > k){
  24.             ms.erase(ms.find(a[l]));
  25.             l++;
  26.         }
  27.         ans += (i - l + 1);
  28.     }
  29.     cout << ans << '\n';
  30. }
  31.  
  32.  
  33.  
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement