poj1089:Intervals

本文探讨了一种解决区间合并问题的算法,通过排序和遍历,实现区间最小数量的合并,避免了复杂的树结构操作,提高了效率。提供了一个实际的代码示例,详细解释了解题思路和步骤。

1089:Intervals

总时间限制: 1000ms 内存限制: 65536kB
描述
There is given the series of n closed intervals [ai; b i], where i=1,2,...,n. The sum of those intervals may berepresented as a sum of closed pairwise non−intersecting intervals. The task is to find such representation with the minimal number of intervals. The intervals of this representation should be written in the output file in acceding order. We say that the intervals [a; b] and [c; d] are in ascending order if, and only if a <= b < c <= d.
Task
Write a program which:
.reads from the std input the description of the series of intervals,
.computes pairwise non−intersecting intervals satisfying the conditions given above, 
.writes the computed intervals in ascending order into std output
输入
In the first line of input there is one integer n, 3 <= n <= 50000. This is the number of intervals. In the (i+1)−st line, 1 <= i <= n, there is a description of the interval [ai; bi] in the form of two integers ai and bi separated by a single space, which are respectively the beginning and the end of the interval,1 <= ai <= bi <= 1000000.
输出
The output should contain descriptions of all computed pairwise non−intersecting intervals. In each line should be written a description of one interval. It should be composed of two integers, separated by a single space, the beginning and the end of the interval respectively. The intervals should be written into the output in ascending order.
样例输入
5
5 6
1 4
10 10
6 9
8 10
样例输出
1 4
5 10

这道题做的时候真是无语了。。。。

这几天做的线段树的题太多了,导致特么一看到线段区间什么的,第一时间就想到线段树来解。。。结果写了一个小时,觉得不对劲,1000000个叶子节点的树的数据大小。。。光是建树估计花的时间就超时了。。。最开始想的方法是建树,再用consult来查询是否被覆盖,并根据左端点来排序,用二分来找出刚好被覆盖到的满足条件的最大的右端点。。。

快写哭了。。。实在没信心写不下去了,百度了一下诸位大侠的写法,才发现自己简直蠢到家了。。。

最后解法:

直接简单地根据左端点由小到大排好序,然后遍历过去就OK了,并把重叠的合在一起,直到发现并没有交叉的区间,然后输出之前的区间,最后把最后一次的区间输出就好了。

代码:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 
 7 struct P
 8 {
 9     int l;
10     int r;
11 }p[50001];
12 
13 bool cmp(const P& a,const P& b)
14 {
15     return a.l < b.l;
16 }
17 
18 int main()
19 {
20     int n;
21     scanf("%d",&n);
22     for (int i = 0; i < n; ++i)
23     {
24         scanf("%d%d",&p[i].l,&p[i].r);
25     }
26     sort(p,p+n,cmp);
27     int x = p[0].l,y = p[0].r;
28     for(int i = 0; i < n; ++i)
29     {
30         if(p[i].l >= x && p[i].l <=y)
31         {
32             if(p[i].r > y)
33                 y = p[i].r;
34         }
35         else
36         {
37             printf("%d %d\n",x,y);
38             x = p[i].l;
39             y = p[i].r;
40         }
41     }
42     printf("%d %d\n",x,y);
43     return 0;
44 }
View Code

 

 

转载于:https://www.cnblogs.com/xiaoshen555/p/3819277.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值