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; }
相关文章推荐
- 51nod 2级算法题-1126
- 51nod 2级算法题-1092
- 51nod 2级算法题-1279
- 51nod 2级算法题-1095
- 51nod 2级算法题-1315
- 【贪心+优先队列】1428 活动安排问题【51nod】【难度:2级算法题】
- 51nod 2级算法题-1119
- 【模拟】1432 独木舟【51nod】【难度:2级算法题】
- 51nod 2级算法题-1428
- 51nod 2级算法题-1007
- 51nod 2级算法题-1010
- 51nod 2级算法题-1014
- 51nod 2级算法题-1050
- 51nod 2级算法题-1062
- [最短路 主席树 Hash] 51Nod 算法马拉松26 E Travel
- 51nod2006 飞行员配对(二分图最大匹配)匈牙利算法
- 贪心算法基础之独木舟问题 51nod 贪心教程
- 51NOD 1632 B君的连通(推公式)——算法马拉松16
- 一个台阶总共有n级,如果一次可以跳1级,也可以跳2级。 求总共有多少总跳法,并分析算法的时间复杂度。
- [状态压缩||折半搜索]51nod 算法马拉松3 A