zoukankan      html  css  js  c++  java
  • cf 1029 C

    C. Maximal Intersection
    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given nn segments on a number line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide.

    The intersection of a sequence of segments is such a maximal set of points (not necesserily having integer coordinates) that each point lies within every segment from the sequence. If the resulting set isn't empty, then it always forms some continuous segment. The length of the intersection is the length of the resulting segment or 00 in case the intersection is an empty set.

    For example, the intersection of segments [1;5][1;5] and [3;10][3;10] is [3;5][3;5] (length 22), the intersection of segments [1;5][1;5] and [5;7][5;7] is [5;5][5;5] (length 00) and the intersection of segments [1;5][1;5] and [6;6][6;6] is an empty set (length 00).

    Your task is to remove exactly one segment from the given sequence in such a way that the intersection of the remaining (n1)(n−1) segments has the maximal possible length.

    Input

    The first line contains a single integer nn (2n31052≤n≤3⋅105) — the number of segments in the sequence.

    Each of the next nn lines contains two integers lili and riri (0liri1090≤li≤ri≤109) — the description of the ii-th segment.

    Output

    Print a single integer — the maximal possible length of the intersection of (n1)(n−1) remaining segments after you remove exactly one segment from the sequence.

    Examples
    input
    Copy
    4
    1 3
    2 6
    0 4
    3 3
    output
    Copy
    1
    input
    Copy
    5
    2 6
    1 3
    0 4
    1 20
    0 4
    output
    Copy
    2
    input
    Copy
    3
    4 5
    1 2
    9 20
    output
    Copy
    0
    input
    Copy
    2
    3 10
    1 5
    output
    Copy
    7
    Note

    In the first example you should remove the segment [3;3][3;3], the intersection will become [2;3][2;3] (length 11). Removing any other segment will result in the intersection [3;3][3;3] (length 00).

    In the second example you should remove the segment [1;3][1;3] or segment [2;6][2;6], the intersection will become [2;4][2;4] (length 22) or [1;3][1;3] (length 22), respectively. Removing any other segment will result in the intersection [2;3][2;3] (length 11).

    In the third example the intersection will become an empty set no matter the segment you remove.

    In the fourth example you will get the intersection [3;10][3;10] (length 77) if you remove the segment [1;5][1;5] or the intersection [1;5][1;5] (length 44) if you remove the segment [3;10][3;10].

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstdlib>
     5 #include <cstring>
     6 #include <string>
     7 #include <deque>
     8 #include <vector>
     9 #include <set>
    10 #include <map>
    11 using namespace std;
    12 #define ll long long 
    13 #define  N 300009
    14 int n,a[N],b[N],c[N],d[N];
    15 map<int,int>mp;
    16 int maxx=-1,minn=1000000009;
    17 bool check(int x,int y)
    18 {
    19     if(x!=y||x==y&&mp[y]>1)
    20         return true;
    21     return false;
    22 }
    23 int main()
    24 {
    25     scanf("%d",&n);
    26     for(int i=1;i<=n;i++){
    27         scanf("%d%d",&a[i],&b[i]);
    28         c[i]=a[i];d[i]=b[i];
    29         mp[a[i]]++;mp[b[i]]++;
    30         maxx=max(maxx,a[i]);
    31         minn=min(minn,b[i]);
    32     }
    33     //cout<<maxx<<" "<<minn<<endl;
    34     sort(a+1,a+1+n);
    35     sort(b+1,b+1+n);
    36     int l=0;
    37     for(int i=1;i<=n;i++){
    38         //cout<<maxx<<" "<<minn<<" "<<c[i]<<" "<<d[i]<<endl;
    39         if(c[i]==maxx&&check(d[i],minn)){
    40          l=max(l,minn-a[n-1]);
    41         }
    42         if(d[i]==minn&&check(c[i],maxx)){
    43             l=max(l,b[2]-maxx);
    44         }
    45         if(c[i]==maxx&&d[i]==minn){//去掉某一个区间,那么区间的左右要同时去掉
    46             //cout<<l<<endl;
    47             l=max(l,b[2]-a[n-1]);
    48             //cout<<b[2]<<" "<<a[n-1]<<endl;
    49         }
    50     }
    51     /*
    52     2
    53     1 5
    54     2 4
    55     去掉2 4
    56      2 4要同时去掉
    57     */
    58     printf("%d
    ",l);
    59     return 0;        
    60 }
  • 相关阅读:
    CMDXP.CMD命令大全
    ASP.NET 页生命周期概述
    使用 ASP.NET 2.0提供的WebResource管理资源
    软件包管理 之 Fedora / Redhat 软件包管理指南
    开源代码分析研究 之 BugNet (2008年1月14日更新 第一章BugNet 简介 已完成)
    软件包管理 之 如何编译安装源码包软件
    硬件管理 之 Linux 硬件管理的基础知识
    软件包管理 之 fedorarpmdevtools 工具介绍
    软件包管理 之 Fedora/Redhat 在线安装更新软件包,yum 篇 ── 给新手指南
    系统引导管理 之 系统引导过程及硬盘分区结构论述
  • 原文地址:https://www.cnblogs.com/tingtin/p/9536632.html
Copyright © 2011-2022 走看看