当前位置: 首页 > news >正文

大根堆加小根堆查找中位数o(N)时间复杂度

#include<bits/stdc++.h>

using namespace std;

#define maxn 501

int a[maxn]; // 大根堆

int b[maxn]; // 小根堆

int i=0,j=0; // i是大根堆大小,j是小根堆大小

void swap(int i,int j,int *a){

    int tmp=a[i];

    a[i]=a[j];  

    a[j]=tmp;

}

void Bigheapinsert(int x){

    a[i] = x;

    int m = i++;

    while(m > 0){

        int n = (m-1)/2;

        if(a[n] >= a[m]) break;

        swap(m,n,a);

        m = n;

    }

}

void Smallheapinsert(int x){

    b[j] = x;

    int m = j++;

    while(m > 0){

        int n = (m-1)/2;

        if(b[n] <= b[m]) break;

        swap(m,n,b);

        m = n;

    }

}

void Bigheapify(){

    int pos = 0;

    while(true){

        int left = 2*pos+1;

        int right = 2*pos+2;

        int largest = pos;

       

        if(left < i && a[left] > a[largest]) largest = left;

        if(right < i && a[right] > a[largest]) largest = right;

       

        if(largest == pos) break;

       

        swap(pos, largest, a);

        pos = largest;

    }

}

void Smallheapify(){

    int pos = 0;

    while(true){

        int left = 2*pos+1;

        int right = 2*pos+2;

        int smallest = pos;

       

        if(left < j && b[left] < b[smallest]) smallest = left;

        if(right < j && b[right] < b[smallest]) smallest = right;

       

        if(smallest == pos) break;

       

        swap(pos, smallest, b);

        pos = smallest;

    }

}

int main(){

    int n;

    cin>>n;

    int arr[n];

    for(int k=0;k<n;k++){

        cin>>arr[k];

    }

   

    for(int k=0;k<n;k++){

        if(i+j == 0 || arr[k] <= a[0]){

            Bigheapinsert(arr[k]);

        }else{

            Smallheapinsert(arr[k]);

        }

       

        // 平衡堆大小

        if(i-j > 1){

            int val = a[0];

            a[0] = a[--i];

            Bigheapify();

            Smallheapinsert(val);

        }

        else if(j-i > 1){

            int val = b[0];

            b[0] = b[--j];

            Smallheapify();

            Bigheapinsert(val);

        }

    }

   

    double ans = -1;

    if((i+j)%2 == 0){

        ans =(double) (a[0]+b[0])/2;

    }else{

        ans =(double) ((i > j) ? a[0] : b[0]);

    }

    cout<<"ans = "<<ans<<endl;

    return 0;

}

http://www.lryc.cn/news/578295.html

相关文章:

  • 【Springai】项目实战进度和规划
  • DFMEA检查表模板下载
  • PHP安装使用教程
  • js代码02
  • 【C++】简单学——模板初阶
  • PyTorch 中 nn.Linear() 参数详解与实战解析(gpt)
  • 项目:数据库应用系统开发:智能电商管理系统
  • 认识 Spring AI
  • 【C++】简单学——STL简介(了解)
  • tauri v2 开源项目学习(一)
  • 安装bcolz包报错Cython.Compiler.Errors.CompileError: bcolz/carray_ext.pyx的解决方法
  • Android Telephony 网络状态中的 NAS 信息
  • 实战避坑:MyBatis中${}拼接如何优雅又安全?
  • RocketMQ第五节(springboot整合MQ)
  • C++ STL之string类
  • Spring 依赖注入:官方推荐方式及最佳实践
  • SpringBoot -- 自动配置原理
  • 高并发限流方案
  • demo01:基于 SpringMVC 的用户管理系统
  • 深入 ARM-Linux 的系统调用世界
  • Windows11系统中安装docker并配置docker镜像到pycharm中
  • 反射,枚举和lambda表达式
  • SpringBoot 启动入口深度解析:main方法执行全流程
  • 初等变换 线性代数
  • python中学物理实验模拟程序系列目录
  • Oracle 树形统计再进阶:类型多样性与高频类型分析(第三课)
  • 长短期记忆网络(LSTM):让神经网络拥有 “持久记忆力” 的神奇魔法
  • CppCon 2018 学习:An allocator is a handle to a heap Lessons learned from std::pmr
  • 【FineDataLink快速入门】01界面介绍-运维中心
  • jvm 锁升级机制