codevs 1214 线段覆盖

1214 线段覆盖

 

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

    给定x轴上的N(0<N<100)条线段,每个线段由它的二个端点a_I和b_I确定,I=1,2,……N.这些坐标都是区间(-999,999)的整数。有些线段之间会相互交叠或覆盖。请你编写一个程序,从给出的线段中去掉尽量少的线段,使得剩下的线段两两之间没有内部公共点。所谓的内部公共点是指一个点同时属于两条线段且至少在其中一条线段的内部(即除去端点的部分)。

输入描述 Input Description

    输入第一行是一个整数N。接下来有N行,每行有二个空格隔开的整数,表示一条线段的二个端点的坐标。

输出描述 Output Description

    输出第一行是一个整数表示最多剩下的线段数。

样例输入 Sample Input

3

6  3

1  3

2  5

样例输出 Sample Output

2

数据范围及提示 Data Size & Hint

0<N<100

贪心,二元组按结束坐标升序,循环1-n记录当前尾指针最大值判断下一个头指针是否与其重叠

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 using namespace std;
 5 pair<int,int>p[1000001];
 6 typedef pair<int, int> pi;
 7 
 8 bool cmp(pi a,pi b)
 9 {
10     if(a.second<b.second)return 1;
11     else return 0;
12     
13 }
14 
15 int main()
16 {
17     int n;
18     scanf("%d",&n);
19     int x;
20     int a,b;
21     for(int i=1;i<=n;i++)
22     {
23         scanf("%d%d",&a,&b);
24         if(a>b)swap(a,b);
25         p[i].first=a;p[i].second=b;
26     }
27     sort(p+1,p+n+1,cmp);
28     int maxn=-1000001,ans=0;
29     for(int i=1;i<=n;i++)
30     {
31         if(p[i].first>=maxn)
32         {    
33             ans++;maxn=p[i].second;
34         }
35         
36     }
37     cout<<ans;
38 }

 

posted @ 2017-05-05 20:07  zzzzx  阅读(146)  评论(0编辑  收藏  举报