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);        
        }
    }
    

      

  • 相关阅读:
    零基础学习接口测试-接口文档
    零基础学习接口测试-HTTP消息结构
    零基础学习接口测试—HTTP协议基础知识
    零基础学习接口测试-接口测试基础知识
    MySQL数据库安装
    软件测试基础-软件测试基本流程
    零基础学习接口测试-HTTPS协议
    软件测试基础-软件测试分类
    BLE5.2 CIS Data Flush Point点SN和NESN变化机制探讨
    使用Jlink给CC2640R2F烧写固件方法
  • 原文地址:https://www.cnblogs.com/wxjor/p/6925946.html
Copyright © 2011-2022 走看看