fork(2) download
  1. #include <vector>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <utility>
  5.  
  6. using namespace std;
  7.  
  8. vector<pair<int, int>> v{ { 1, 3 }, { 2, 5 }, { 6, 9 } };
  9.  
  10. int solve(){
  11. vector<pair<int, int>> results;
  12.  
  13. for (auto& vIndex : v){
  14. auto resultIndex = find_if(results.begin(), results.end(), [vIndex](const pair<int, int>& i){return vIndex.first >= i.first && vIndex.first <= i.second || vIndex.second >= i.first && vIndex.second <= i.second; });
  15.  
  16. if (resultIndex == results.end()){
  17. results.push_back(vIndex);
  18. }else{
  19. resultIndex->first = min(vIndex.first, resultIndex->first);
  20. resultIndex->second = max(vIndex.second, resultIndex->second);
  21. }
  22. }
  23. return results.size();
  24. }
  25.  
  26. int main(){
  27. cout << solve() << endl;
  28.  
  29. return 0;
  30. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
2