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

1214 线段覆盖

时间:2019-01-26 17:05:19      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:http   print   font   接下来   一个   时间   提示   div   std   

题目链接http://codevs.cn/problem/1214/

相似题目https://www.cnblogs.com/huashanqingzhu/p/6728674.html

时间限制: 1 s     空间限制: 128000 KB
题目描述 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 #include <stdio.h>
 2 #include <stdlib.h>
 3 struct line
 4 {
 5     int begin,end;
 6 };
 7 int cmp(const void *xx,const void *yy)//按线段的终点从小到大排序。终点相同的,则按起点从大到小排序
 8 {
 9     struct line a=*(struct line *)xx;
10     struct line b=*(struct line *)yy;
11     if(a.end<b.end) return -1;
12     else if(a.end>b.end) return 1;
13     else
14     {
15         if(a.begin>b.begin)return -1;
16         else if(a.begin<b.begin) return 1;
17         else return 0;
18     }
19 }
20 int main()
21 {
22     int n,i,t;
23     struct line a[103];
24     int lastEnd,count=0;
25     scanf("%d",&n);
26     for(i=0;i<n;i++)
27     {
28         scanf("%d%d",&a[i].begin,&a[i].end);
29         if(a[i].begin>a[i].end)
30         {
31             t=a[i].begin;a[i].begin=a[i].end;a[i].end=t;
32         }
33     }
34     qsort(a,n,sizeof(a[0]),cmp);
35     /*for(i=0;i<n;i++) printf("%d %d\n",a[i].begin,a[i].end);*/
36     lastEnd=-1500;
37     for(i=0;i<n;i++)
38     {
39         if(a[i].begin>=lastEnd)
40             {lastEnd=a[i].end;count++;}
41     }
42     printf("%d",count);
43     return 0;
44 }

 

 

1214 线段覆盖

标签:http   print   font   接下来   一个   时间   提示   div   std   

原文地址:https://www.cnblogs.com/huashanqingzhu/p/10323739.html

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