The main topic: There are n length of 1 line segment, n interval, the section I segment in the first interval, ask the minimum gap between the lines there are several.
Title Analysis: First to the interval sort, first by the right end row, and then by the left end row. There must be no gap between overlapping intervals (where only intersection overlaps are considered overlapping). Greedy strategy: Each encounter a new interval (no overlap with the front interval), then the segment is placed in the last length of the interval of 1, if not the new range can be directly ignored.
The code is as follows:
# include<iostream># include<cstdio># include<cstring># include<algorithm>using namespace std;struct q{ int l,r; BOOL operator < (const Q &a) Const { if (R==A.R) return l<a.l; Return r<a.r; }}; Q a[100005];void Solve (int n) { int ans=0,p=-1; for (int i=0;i<n;++i) { if (a[i].r==p) continue; if (a[i].l>p) { ++ans; P=A[I].R; } else ++p; } printf ("%d\n", ans-1);} int main () { int t,n; scanf ("%d", &t); while (t--) { scanf ("%d", &n); for (int i=0;i<n;++i) scanf ("%d%d", &A[I].L,&A[I].R); Sort (a,a+n); Solve (n); } return 0;}
UVA-1617 Laptop (greedy)