fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. int main() {
  7. const int L = 5;
  8. vector<int> v = { 1, 10, 11, 2 };
  9. const int N = v.size();
  10.  
  11. sort(v.begin(), v.end());
  12. // the algo tries to include elements in interval of length 2 * L
  13. int ans = 0;
  14. int first = 0;
  15. for(int i = 1; i < N; ++i) {
  16. if(v[i] - v[first] > 2 * L) { // if we can't include i-th element
  17. ans++; // into the current interval
  18. first = i; // the algo construct new
  19. }
  20. }
  21. ans++;
  22. printf("%d", ans);
  23. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
1