fork download
  1. #include <iostream>
  2. #include<vector>
  3. #include<set>
  4. using namespace std;
  5.  
  6. int main() {
  7. vector<int> nums={2,6,4,3,6,8,9};
  8. int low=0;
  9. int high=0;
  10. int count=0;
  11. int k=3;
  12. multiset<int> s;
  13.  
  14. for(int high=0;high<nums.size();high++) {
  15. s.insert(nums[high]);
  16. while(*s.rbegin()-*s.begin()>k) {
  17. s.erase(s.find(nums[low]));
  18. low++;
  19. }
  20. count+=high-low+1;
  21. }
  22. cout<<count<<endl;
  23. return 0;
  24. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
16