zoukankan      html  css  js  c++  java
  • 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
    ",x,y);
    38             x = p[i].l;
    39             y = p[i].r;
    40         }
    41     }
    42     printf("%d %d
    ",x,y);
    43     return 0;
    44 }
    View Code
  • 相关阅读:
    TCP/IP讲解
    Android开发的技术层次
    页面右下角弹出类似QQ或MSN的消息提示
    C# winform 自定义鼠标图标
    C#遍历指定文件夹中的所有文件
    C#操作Word
    关于数据绑定的一些小技巧
    Silverlight遍历本地文件夹
    ckeditor+ckfinder+syntaxhighlight实现上传和插入代码高亮(for .NET)
    wpf 动画效果
  • 原文地址:https://www.cnblogs.com/xiaoshen555/p/3819277.html
Copyright © 2011-2022 走看看