码迷,mamicode.com
首页 > 其他好文 > 详细

1643 线段覆盖 3

时间:2017-04-27 12:35:45      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:const   nbsp   problem   sort   stream   space   rom   cst   input   

1643 线段覆盖 3

 

时间限制: 2 s
空间限制: 256000 KB
题目等级 : 黄金 Gold
 
 
 
题目描述 Description

在一个数轴上有n条线段,现要选取其中k条线段使得这k条线段两两没有重合部分(端点可以重合),问最大的k为多少。

 

 

 

输入描述 Input Description

输入格式

输入文件的第1行为一个正整数n,下面n行每行2个数字ai,bi,描述每条线段。

输出描述 Output Description

输出格式

  输出文件仅包括1个整数,为k的最大值

样例输入 Sample Input

3

0 2

2 4

1 3

样例输出 Sample Output

2

数据范围及提示 Data Size & Hint

数据范围

对于20%的数据,n≤10;

对于50%的数据,n≤1000;

对于70%的数据,n≤100000;

对于100%的数据,n≤1000000,0≤ai<bi≤1000000。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 using namespace std;
 5 const int N=1000001;
 6 struct node{
 7     int q,h;
 8 }s[N];
 9 bool cmp(node a,node b)
10 {
11     return a.h<b.h;
12 }
13 int main()
14 {
15     int n;
16     cin>>n;
17     for(int i=1;i<=n;i++)
18      {
19          cin>>s[i].q>>s[i].h;
20          if(s[i].q>s[i].h)swap(s[i].q,s[i].h);
21      }
22      sort(s+1,s+n+1,cmp);
23      int ans=0;
24      for(int i=1;i<=n-1;i++)
25       {
26           if(s[i].h>s[i+1].q)
27            {
28                ans++;
29                s[i+1].h=s[i].h;
30            }
31       }
32       cout<<n-ans;
33 }

 

1643 线段覆盖 3

标签:const   nbsp   problem   sort   stream   space   rom   cst   input   

原文地址:http://www.cnblogs.com/lyqlyq/p/6773428.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!