zoukankan      html  css  js  c++  java
  • POJ1089 Intervals

    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

     
     
    正解:贪心+排序
    解题报告:
      水题。区间覆盖,排一遍序即可。
     
     1 //It is made by jump~
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <algorithm>
     8 #include <ctime>
     9 #include <vector>
    10 #include <queue>
    11 #include <map>
    12 #ifdef WIN32   
    13 #define OT "%I64d"
    14 #else
    15 #define OT "%lld"
    16 #endif
    17 using namespace std;
    18 typedef long long LL;
    19 const int MAXN = 50011;
    20 int n;
    21 
    22 struct seq{
    23     int l,r;
    24 }a[MAXN];
    25 
    26 inline bool cmp(seq q,seq qq){ if(q.l==qq.l) return q.r<qq.r; return q.l<qq.l; }
    27 
    28 inline int getint()
    29 {
    30        int w=0,q=0;
    31        char c=getchar();
    32        while((c<'0' || c>'9') && c!='-') c=getchar();
    33        if (c=='-')  q=1, c=getchar();
    34        while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
    35        return q ? -w : w;
    36 }
    37 
    38 
    39 inline void solve(){
    40     n=getint();
    41     for(int i=1;i<=n;i++) {
    42     a[i].l=getint(); a[i].r=getint();
    43     }
    44     sort(a+1,a+n+1,cmp);    
    45     int nowl=a[1].l,nowr=a[1].r;
    46     for(int i=2;i<=n;i++) {
    47     if(a[i].l>nowr) {
    48         printf("%d %d
    ",nowl,nowr);
    49         nowl=a[i].l; nowr=a[i].r;
    50     }
    51     else if(a[i].r>nowr)  nowr=a[i].r;    
    52     }
    53 
    54     printf("%d %d",nowl,nowr);
    55 }
    56 
    57 int main()
    58 {
    59   solve();
    60   return 0;
    61 }
  • 相关阅读:
    Masonry介绍与使用实践:快速上手Autolayout
    OC文件大小的计算方法,多用于清理缓存
    OC接收数据时毫秒转date时间最简略方法
    使用OC语言编写两个超大数相乘或相加的算法的思路和超大正整数相乘的代码
    简述AFN(AFNetWorking 2.X)的实现分析和简单使用【转】
    iOS开发中xib和Storyboard中需要注意的事项
    如何将UISearchBar上"Cancel"按钮改为”取消“?
    iOS开发中关于本地数据中SQLite数据库常用的SQL语句
    UItableViewCell上的button点击无响应的办法
    UIScrollView的几个要点总结
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/5605087.html
Copyright © 2011-2022 走看看