Advertisement
Korotkodul

1E

Sep 28th, 2023 (edited)
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cmath>
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. using std::cin;
  7. using std::cout;
  8. using std::max;
  9. using std::min;
  10. using std::string;
  11. using std::vector;
  12. using Pii = std::pair<int, int>;
  13.  
  14. bool Cmp(Pii aa, Pii bb) {
  15.   return aa.first < bb.first || (aa.first == bb.first && aa.second > bb.second);
  16. }
  17.  
  18. int main() {
  19.   std::ios::sync_with_stdio(false);
  20.   std::cin.tie(0);
  21.   std::cout.tie(0);
  22.   int nn;
  23.   cin >> nn;
  24.   vector<Pii> aa;
  25.   for (int ii = 0; ii < nn; ++ii) {
  26.     int li;
  27.     int ri;
  28.     cin >> li >> ri;
  29.     aa.push_back({li, 1});
  30.     aa.push_back({ri, -1});
  31.   }
  32.   sort(aa.begin(), aa.end(), Cmp);
  33.   /*cout << "SORTED\n";
  34.   for (auto pp: aa) {
  35.     cout << pp.first << ' ' << pp.second << "\n";
  36.   }*/
  37.   vector<Pii> ans;
  38.   int bal = 0;
  39.   int lbound;
  40.   int rbound;
  41.   for (int ii = 0; ii < nn * 2; ++ii) {
  42.     bal += aa[ii].second;
  43.     if (bal == 1 && aa[ii].second == 1) {
  44.       lbound = aa[ii].first;
  45.       // cout << "l r = " << lbound << ' ' << rbound << "\n";
  46.     }
  47.     if (bal == 0) {
  48.       rbound = aa[ii].first;
  49.       // cout << "l r = " << lbound << ' ' << rbound << "\n";
  50.       ans.push_back({lbound, rbound});
  51.     }
  52.   }
  53.   cout << ans.size() << "\n";
  54.   for (auto pp : ans) {
  55.     cout << pp.first << ' ' << pp.second << "\n";
  56.   }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement