题意:

给定N个初始值为0的数, 然后给定K个区间修改(区间[l,r] 每个元素加一), 求修改后序列的中位数。

分析:

K个离线的区间修改可以使用差分数组(http://www.cnblogs.com/Jadon97/p/8053946.html)实现。

 

关于对一个无序的序列找出中位数

方法一:

 第一时间想到的方法是快排然后之间取中位数, 排序复杂度为O(NlogN),取第k大的数复杂度O(1)。

用时:124ms

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000 + 7;
int N, K;
int a[maxn], d[maxn];
int main(){
    cin >> N >> K;
    d[1] = a[1];
    for(int i = 0; i < K; i++){
        int a, b;
        cin >> a >> b;
        d[a]++, d[b+1]--;
    }
    for(int i = 1; i <= N; i++){
        a[i] = a[i-1] + d[i];
    }
    sort(a+1,a+1+N);
    cout << a[N/2+1] << "\n";
    return 0;
}
快排

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-02
  • 2021-12-19
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-05
  • 2022-12-23
  • 2021-11-21
  • 2021-11-20
相关资源
相似解决方案