fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool cmp(const pair<int, int>& a, const pair<int, int>& b) {
  5. if(a.first != b.first)
  6. return a.first < b.first;
  7.  
  8. return a.second > b.second;
  9. }
  10.  
  11. int main() {
  12. ios_base::sync_with_stdio(false), cin.tie(0);
  13.  
  14. int n, a, b;
  15. set<pair<int, int>, decltype(cmp)*> S(cmp);
  16.  
  17. cin >> n;
  18. while(n--) {
  19. cin >> a >> b;
  20.  
  21. S.insert({b, a});
  22. }
  23.  
  24. int ans = 0;
  25.  
  26. while(!S.empty()) {
  27. auto it = S.begin();
  28.  
  29. while(it != S.end()) {
  30. cout << it -> first << ' ' << it -> second << '\n';
  31.  
  32. auto nxtIt = S.lower_bound({it -> first + 1, it -> second});
  33.  
  34. S.erase(it);
  35. it = nxtIt;
  36. }
  37.  
  38. ans++;
  39. }
  40.  
  41. cout << ans;
  42. }
Success #stdin #stdout 0s 5304KB
stdin
5
172 1
161 2
188 4
154 2
180 1
stdout
1 180
2 161
4 188
1 172
2 154
2