您的位置:首页 > 其它

51nod 2级算法题-1133

2017-04-25 15:55 183 查看

1133 不重叠的线段

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 <cstring>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
#define endl "\n"
const int maxn=10000+100;
struct Line{
int l;
int r;
}L[maxn];
bool cmp(Line A,Line B){
if(A.r==B.r){
return A.l<B.l;
}
return A.r<B.r;
}
int main (){
ios::sync_with_stdio(false);
int a=L[0].r;
for(int i=1;i<n;i++){
if(L[i].l>=a){
a=L[i].r;
sum++;
}
}
cout<<sum<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: