There are N line segments on the X-axis, and each line segment has a start point S and an end point E. The maximum number of non-overlapping line segments that can be selected. (Note: The starting point or the ending point overlaps, and it is not counted as overlapping).
For example: [1 5][2 3][3 6], you can choose [2 3][3 6], these two line segments do not overlap each other.
Input
Line 1: 1 number N, number of line segments (2 <= N <= 10000) Lines 2 - N + 1: 2 numbers per line, start and end of the line segment (-10^9 <= S, E <= 10^9)
Output
Output the maximum number of line segments that can be selected.
Input example
3 1 5 2 3 3 6
Output example
2
AC: code
#include<stack> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; struct edge{ int st,en; }ss[10005]; int cmp(edge x,edge y){ if(x.en == y.en) return x.st<y.st; return x.in<y.in; } intmain() { int t; cin>>t; for(int i = 0;i<t;i++) { scanf("%d %d",&ss[i].st,&ss[i].en); }sort(ss,ss+t,cmp); stack<edge>v; v.push(ss[0]); for(int i = 1;i<t;i++){ edge e = v.top (); if(e.en>ss[i].st){ continue; } else { v.push(ss[i]); } } printf("%d\n",v.size()); return 0; }