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 }
  • 相关阅读:
    read、readline、readlines和linecache的使用
    无法启用internet连接共享,为LAN连接配置的IP地址需要使用自动IP寻址
    虚拟机pycharm
    Ubuntu安装谷歌浏览器
    pandas dataframe重复数据查看.判断.去重
    git 删除误上传的.idea文件
    python logger日志通用配置文件
    pyinstaller打包python文件成exe(原理.安装.问题)
    SSH 免密登录服务器
    homebrew安装和解决brew安装速度慢的问题
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/5605087.html
Copyright © 2011-2022 走看看