codeup28007 Integer Range __ Greedy

Source: Internet
Author: User
codeup28007 integer Interval

Time limit 1000MS/128MB
Topic Description

Read the number of closed intervals from the file and their description. Programming to find a set with the least number of elements, so that for each interval, at least one integer belongs to the collection, outputting the number of elements of the collection. input

The first row includes the number of intervals n,1<=n<=10000, the next n rows, each containing two integers a,b, separated by a single space, 0<=a<=b<=10000, which is the starting and ending values of an interval. Output

The number of collection elements, and for each interval there is at least one integer that belongs to the collection, and the collection contains the least number of elements. Sample Input

4
3 6
2 4
0 2
4 7
Sample Output
2


Code

 #include <iostream> #include <algorithm> using namespace std; const int N = 10005; struct node {    int str,end     bool operator < (const node &x) const{     
;   return end<x.end;
    };

Node A[n]; int main () {    int n,ans=0     cin>>n;     for (int i=1; i<=n; i++
) cin>>a[i].str>>a[i].end;
    sort (a+1,a+n+1);     for (int i=1,x=-1 i<=n; i++)         if (a[i].str>x) ans++,x=a[i
].end;
    cout<<ans<<endl;
    return 0; }

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.