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

      

  • 相关阅读:
    源码解析之–网络层YTKNetwork
    ARC和非ARC文件混编
    Xcode真机调试中"There was an internal API error"错误解决方法
    设置textView或者label的行间距方法
    iOS中__block 关键字的底层实现原理
    使用AVCaptureSession捕捉静态图片
    使用AVCaptureSession显示相机预览
    短小强悍的JavaScript异步调用库
    开源中国愚人节网页变模糊的js blur代码
    undefined与null的区别
  • 原文地址:https://www.cnblogs.com/wxjor/p/6925946.html
Copyright © 2011-2022 走看看