网页设计与制作html/泾县网站seo优化排名
1133 不重叠的线段
基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注
X轴上有N条线段,每条线段有1个起点S和终点E。最多能够选出多少条互不重叠的线段。(注:起点或终点重叠,不算重叠)。
例如:[1 5][2 3][3 6],可以选[2 3][3 6],这2条线段互不重叠。
Input
第1行:1个数N,线段的数量(2 <= N <= 10000)
第2 - N + 1行:每行2个数,线段的起点和终点(-10^9 <= S,E <= 10^9)
Output
输出最多可以选择的线段数量。
Input示例
3
1 5
2 3
3 6
Output示例
2
贪心
#include<iostream>
#include<algorithm>
using namespace std;
struct Line
{int start,endless;
};
struct Line L[10005];
bool cmp(struct Line a,struct Line b)
{return a.endless<b.endless;
}
int main()
{int n;cin>>n;for (int i=0;i<n;i++)cin>>L[i].start>>L[i].endless;sort(L,L+n,cmp);int sum=1;int temp=L[0].endless;for(int i=1;i<n;i++){if (temp<=L[i].start){sum++;temp=L[i].endless;}}cout<<sum;return 0;
}