• Aizu


    给你n个区间,求:

    1:最多有多少区间与同一个区间相交。

    2:相交部分的最大区间数目。

    Sample Input 1
    4
    1 3
    1 3
    3 6
    3 6
    
    Sample Output 1
    2 2
    
    
    Sample Input 2
    4
    1 2
    2 3
    1 3
    3 4
    
    Sample Output 2
    3 2
    
    
    Sample Input 3
    10
    84 302
    275 327
    364 538
    26 364
    29 386
    545 955
    715 965
    404 415
    903 942
    150 402
    
    Sample Output 3
    6 5
    

      

    第二个问题可以直接用差分解决。(区间修改)

    第一个问题:

    对于每一个区间,统计在它左端点前面的右端点的数目,和在它右端点前面的左端点的数目。

    然后与它相交的区间数 = 后者 - 前者。(!!!)

    这样用两个前缀和,一个统计左端点数,一个统计右端点数就可以了。

    #include <cstdio>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    
    const int maxn = 200000 + 100;
    const int N = 100000;
    
    int n;
    int a[maxn], b[maxn];
    int cnt[maxn], up[maxn], down[maxn];
    
    int main()
    {
        scanf("%d", &n);
    
        for (int i = 1; i <= n; i++)
        {
            scanf("%d%d", &a[i], &b[i]);
            cnt[a[i]]++, cnt[b[i]]--;
            up[a[i]]++, down[b[i]]++;
        }
    
        int ans1 = 0, ans2 = 0;
        for (int i = 1; i <= N; i++)
        {
            cnt[i] += cnt[i-1];
            up[i] += up[i-1], down[i] += down[i-1];
            ans2 = max(ans2, cnt[i]);
        }
    
        for (int i = 1; i <= n; i++)
            ans1 = max(ans1, up[b[i]-1] - down[a[i]]);
    
        printf("%d %d
    ", ans1, ans2);
    }
  • 相关阅读:
    Redis源码阅读笔记(2)——字典(Map)实现原理
    Partition List ——LeetCode
    docker format (golang template)
    markdown 换行
    pecan快速教程
    nvdimm
    k8s device plugin
    linux 文件同步
    复制MIFARE Classic卡
    install docker swarm on centos
  • 原文地址:https://www.cnblogs.com/ruthank/p/9544860.html
Copyright © 2020-2023  润新知