按左端点排序,然后维护右端点最大值,贪心的思想。。。明明线扫一遍O(n)就好……然额……还是想得有些复杂
#include#include #include #include #include using namespace std;typedef long long ll;const int MAX = 5e4 + 5;int n;struct line { int l, r;}a[MAX];bool cmp(line a, line b){ if (a.l == b.l) return a.r > b.r; return a.l < b.l;}int main(){ ios::sync_with_stdio(false); while (cin >> n) { for (int i = 0; i < n; i++) cin >> a[i].l >> a[i].r; sort(a, a + n, cmp); int maxx=0,e=a[0].r; for (int i = 1; i < n; i++) { maxx = max(maxx, min(e, a[i].r) - a[i].l); //当前和之前相比较小的右端点-当前左端点 e = max(e, a[i].r); //维护右端点最大值 } cout << maxx << endl; } return 0;}