(基于堆排序的中位数提取 C++实现) Coursera 斯坦福大学 算法课程 Programming Assignment #7

Graph Search, Shortest Paths, and Data Structures

题目

第 1 个问题

Download the following text file:
Median.txt

The goal of this problem is to implement the “Median Maintenance” algorithm (covered in the Week 3 lecture on heap applications). The text file contains a list of the integers from 1 to 10000 in unsorted order; you should treat this as a stream of numbers, arriving one by one. Letting xix_ixi​ denote the iiith number of the file, the kkkth median mkm_kmk​ is defined as the median of the numbers x1,…,xkx_1,\ldots,x_kx1​,…,xk​. (So, if kkk is odd, then mkm_kmk​ is ((k+1)/2)((k+1)/2)((k+1)/2)th smallest number among x1,…,xkx_1,\ldots,x_kx1​,…,xk​; if kkk is even, then mkm_kmk​ is the (k/2)(k/2)(k/2)th smallest number among x1,…,xkx_1,\ldots,x_kx1​,…,xk​.)

In the box below you should type the sum of these 10000 medians, modulo 10000 (i.e., only the last 4 digits). That is, you should compute (m1+m2+m3+⋯+m10000) mod 10000(m_1+m_2+m_3 + \cdots + m_{10000}) \bmod 10000(m1​+m2​+m3​+⋯+m10000​)mod10000.

OPTIONAL EXERCISE: Compare the performance achieved by heap-based and search-tree-based implementations of the algorithm.

程序

#include <iostream>
#include <fstream>
#include <cassert>
#include <string>
#include <cstdlib>
#include <vector>
#include <algorithm>

using namespace std;

class MedianHeap
{
   
public:
    int addOne(int input);
    enum who
    {
   
        upper = 0, lower = 1, both = 2, nobody = 3
    };

private:
    int whoIsEmpty() {
   
        if (upperVec.size() * lowerVec.size() != 0) {
    return nobody; }
        if (upperVec.size() + lowerVec.size() 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值