zoukankan      html  css  js  c++  java
  • 【POJ】1089Intervals

    Intervals
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 8276   Accepted: 3270

    Description

    There is given the series of n closed intervals [ai; bi], where i=1,2,...,n. The sum of those intervals may be represented 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

    Input

    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.

    Output

    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.

    Sample Input

    5
    5 6
    1 4
    10 10
    6 9
    8 10

    Sample Output

    1 4
    5 10

    Source

     
    题意:合并N个区间(有交即合并)
    代码:
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    inline int read(){
        int x=0,f=1;char c=getchar();
        for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
        for(;isdigit(c);c=getchar()) x=x*10+c-'0';
        return x*f;       
    }
    
    int N;
    struct data{
        int fir,sec;
    }a[50001],ans[50001];
    
    bool cmp(data a,data b){
        if(a.fir!=b.fir) return a.fir<b.fir;
        return a.sec<b.sec;     
    }
    
    int main(){
        N=read();
        for(int i=1;i<=N;i++){
            a[i].fir=read();
            a[i].sec=read();        
        }
        sort(a+1,a+N+1,cmp);
        int str=a[1].fir,end=a[1].sec;
        int tmp=0;
        for(int i=2;i<=N;i++){
            if(a[i].fir>end){
                ans[++tmp].fir=str;
                ans[tmp].sec=end;
                str=a[i].fir;
                end=a[i].sec;
                continue;                        
            }
            else end=max(end,a[i].sec);
        }
        ans[++tmp].fir=str;
        ans[tmp].sec=end;
        for(int i=1;i<=tmp;i++){
            printf("%d %d
    ",ans[i].fir,ans[i].sec);        
        }
    }
    

      

  • 相关阅读:
    压测场景下的 TIME_WAIT 处理
    拥抱云原生,Fluid结合JindoFS :阿里云OSS加速利器
    从DHTML、HTC、XHTML到AJAX
    altas(ajax)控件(一):多功能面板控件Accordion
    fedora7 常用软件安装
    Fedora7安装后的配置
    .net程序员的盲点(六):StringBuilder 和 String 的区别
    .net程序员的盲点(五):告诉你一个不一样的new
    .net程序员的盲点(四):索引器Indexers
    员工究竟渴望学到的是什么?-(杂谈-20070816)
  • 原文地址:https://www.cnblogs.com/wxjor/p/6925946.html
Copyright © 2011-2022 走看看