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; }