1091 overlap of segmentsbase time limit: 1 seconds space limit: 131072 KB Score: 5 Difficulty: 1-level algorithm problemthere are n segments on the x-axis, and each segment consists of 1 start and end points. The overlap of the line segments is calculated in this way, [10 20] and [12 25] overlap Part [12 20]. gives the starting and ending points of N line segments, from which 2 segments are selected, and the overlapping parts of the two segments are the longest. Output this maximum distance. If there is no overlap, output 0. Input
Line 1th: Number of segments N (2 <= n <= 50000). 2-n + 1 lines: 2 numbers per line, start and end points for line segments. (0 <= S, e <= 10^9)
Output
Output the length of the longest repeating interval.
Input Example
51 52 42 83 77 9
Output Example
4
The maximum distance is Max (the longest known distance, min (known farthest right endpoint, right end point of this segment), and time complexity is NLOGN.
The code is as follows
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace Std;
struct p{
int l,r;
}NU[100000];
int com (struct p a,struct p b) {
if (A.L!=B.L) return a.l<b.l;
else return a.r>b.r;
}
int main ()
{
int I,j,n,ma (0), B (0), MAXN (0);
cin>>n;
for (i=0;i<n;i++) {
scanf ("%d%d", &NU[I].L,&NU[I].R);
if (NU[I].L>NU[I].R) swap (NU[I].L,NU[I].R);
}
Sort (nu,nu+n,com);
Ma=max (0,NU[0].R-NU[1].L-1);
for (i=0;i<n;i++) {
B=min (MA,NU[I].R)-nu[i].l;
Ma=max (MA,NU[I].R);
Maxn=max (MAXN,B);
}
//
cout<<maxn<<endl;
return 0;
}
Greed-the overlap of line segments