Greed-the overlap of line segments

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.