51nod 1133 不重叠的线段
来源:互联网 发布:知乎 闲书 编辑:程序博客网 时间:2024/06/04 20:07
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<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<queue>using namespace std;#define N 20010struct rng{ int st,ed;}a[N];int Max=1;int cmp(rng x,rng y){ return x.ed<y.ed?1:0;}priority_queue<int>s;int main(){ int i,j,n,m; scanf("%d",&n); for(i=1;i<=n;i++){ scanf("%d %d",&a[i].st,&a[i].ed); } sort(a+1,a+1+n,cmp); int res=1; for(i=2;i<=n;i++){ if(a[1].ed<=a[i].st){ res++; a[1].ed=a[i].ed; } } printf("%d\n",res); return 0;}
1 0
- 51NOD 1133 不重叠的线段
- 51nod 1133 不重叠的线段
- 51nod 1133 不重叠的线段
- 51nod-1133 不重叠的线段
- 51nod 1133 不重叠的线段
- 51Nod 1133 不重叠的线段
- 51nod 1133 不重叠的线段
- 51nod 1133 不重叠的线段
- 51nod 1133 不重叠的线段
- 51nod 1133 不重叠的线段(贪心)
- 51nod 1133 不重叠的线段 贪心
- 51nod 1133 不重叠的线段(贪心)
- 51Nod-1133-不重叠的线段
- 51Nod 1133 不重叠的线段 (区间贪心
- 51nod 1133 不重叠的线段(贪心)
- 51nod 1133 不重叠的线段(贪心)
- 51nod 1133 不重叠的线段(贪心)
- 51nod 1133 不重叠的线段 贪心
- 关于线程的简单介绍
- 我的Android之旅(二十一)---JSON数据解析
- 【基础试题】矩阵问题 Time Limit:10000MS Memory Limit:65536K Total Submit:401 Accepted:267 Case Time Limit:1
- linux MySQL Community Server 5.7.9 更换数据目录位置
- mysql导出xlsqwen文件要包含表头
- 51nod 1133 不重叠的线段
- 机器学习数据集网站
- trafficserver安装配置
- 【基础试题】数组元素的操作 Time Limit:1000MS Memory Limit:65536K Total Submit:441 Accepted:225 Description
- Maven插件的编写
- aapt的常用命令
- HDU2289
- [刷题]算法竞赛入门经典(第2版) 5-5/UVa10391 - Compound Words
- android 权限