当前位置: 首页 > 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.lqws.cn/news/588907.html

相关文章:

  • I/O I/O基本概念与基本I/O函数 6.30
  • CppCon 2018 学习:An allocator is a handle to a heap Lessons learned from std::pmr
  • 第八章IPv4、IPv6、ICMP、ARP、RARP
  • Mysql索引优化
  • 矩阵方程 线性代数
  • 深度学习04 卷积神经网络CNN
  • docker使用容器网络
  • SQL学习笔记5
  • python环境快速搭建
  • springboot中多个定时任务(@Scheduled)如何互不影响
  • jenkins集成sonarqube(使用token进行远程调用)
  • 查看CPU支持的指令集和特性
  • 项目:数据库应用系统开发:智能电商管理系统
  • 华为云Flexus+DeepSeek征文 | 基于华为云Flexus X实例部署Dify平台构建企业行政助手的可用性研究
  • 第 1 课:Flask 简介与环境配置(Markdown 教案)
  • HTML之常用基础标签
  • LeetCode Hot100(图论)
  • CSDN博客大搬家(本地下载markdown合适和图片本地化)
  • Python 爬虫入门教程:Requests 和 BeautifulSoup 实战
  • 设置方法区内存的大小
  • Linux 系统管理:自动化运维与容器化部署
  • 深入理解指针(3)
  • 【甲方安全建设】敏感数据检测工具 Earlybird 安装使用详细教程
  • httpd-devel 与服务无关
  • BERT 模型详解:结构、原理解析
  • AI编程实战:Cursor黑科技全解析
  • RocketMQ第五节(springboot整合MQ)
  • 计算机网络中那些常见的路径搜索算法(一)——DFS、BFS、Dijkstra
  • 从性能优化赛到社区Committer,走进赵宇捷在Apache Fory的成长之路
  • 条件运算符和逗号运算