ural 1306 (heap推排序)

本文介绍了一种利用C++ STL堆解决求解数列中位数问题的方法,通过对比优先队列和STL堆的表现,展示了如何高效地找到数列的中位数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:http://acm.timus.ru/problem.aspx?space=1&num=1306
题意:
找一个数列的中位数。

分析:
这题纯粹是卡内存的题~~,我先是用优先队列,然后MLE了0.0
然后用STL的heap就水过了~

代码:

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;
typedef long long ll;

const int INF = 0x3f3f3f3f;
const int N = 5e3 + 9;
const double PI = acos (-1.0);
int a[125002];
int main() {
   // freopen ("f.txt", "r", stdin);
    int n,x;
    scanf("%d",&n);

    for(int i=0;i<=n/2;i++){
        scanf("%d",&a[i]);
    }
    make_heap(a,a+n/2+1);
    for(int i=n/2+1;i<n;i++){
        scanf("%d",&x);
        if(x>=a[0])continue;
        pop_heap(a,a+n/2+1);
        a[n/2]=x;
        push_heap(a,a+n/2+1);
    }
    if(n&1){
        printf("%d.0\n",a[0]);
    }
    else{
        double ans=0;
        ans+=a[0];pop_heap(a,a+n/2+1);
        ans+=a[0];
        printf("%.1lf\n",ans/2);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值