UVA-1617 Laptop (greedy)

Source: Internet
Author: User

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)

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.