fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. int main() {
  6. int n;
  7. cin >> n;
  8. vector<pair<long long, long long>> a(n);
  9. for (int i = 0; i < n; i++) {
  10. cin >> a[i].first >> a[i].second;
  11. }
  12. sort(a.begin(), a.end());// Sắp xếp theo thời gian bắt đầu
  13.  
  14. long long maxlength = 0;
  15. long long xt = a[0].first, yt = a[0].second;
  16. for (int i = 1; i < n; i++) {
  17. long long x = a[i].first, y = a[i].second;
  18. if(x <= yt){ // thỏa mãn để nối
  19. yt = max(yt, y); // lấy kết thúc dài hơn
  20. } else { // kiểm tra khi không nối được
  21. maxlength = max(maxlength, yt - xt);
  22. xt = x;
  23. yt = y;
  24. }
  25. }
  26. maxlength = max(maxlength, yt - xt); // kiểm tra đoạn cuối cùng
  27.  
  28. cout << maxlength << endl;
  29. return 0;
  30. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
0