51nod 2级算法题-1133

来源:互联网 发布:usb端口禁用破解 编辑:程序博客网 时间:2024/06/17 06:59

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示例

31 52 33 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;}
0 0
原创粉丝点击