0
点赞
收藏
分享

微信扫一扫

干草堆(差分)

正义的杰克船长 2022-01-05 阅读 63
算法

在这里插入图片描述

在这里插入图片描述
使用差分思想运算。
差分思想详见:https://blog.csdn.net/weixin_45629285/article/details/111146240

#include <iostream>
#include <algorithm>
using namespace std;
const int N=1e6+10;
int main()
{
    long long int b[N]={0};
    int n,k;
    cin>>n>>k;
    while(k--)
    {
        int x,y;
        cin>>x>>y;
        int t=x;
        if(x>y) x=y,y=t;
        b[x]++;
        b[y+1]--;
    }
    for(int i=1;i<=n;i++)
        b[i]=b[i]+b[i-1];
    sort(b+1,b+1+n);
    cout<<b[n/2+1];
    return 0;
}

举报

相关推荐

0 条评论