博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
题目:在一个文件中有 10G 个整数,乱序排列,要求找出中位数。内存限制为 2G。只写出思路即可(内存限制为 2G的意思就是,可以使用2G的空间来运行程序,而不考虑这台机器上的其他软件的占用内存)。
阅读量:4287 次
发布时间:2019-05-27

本文共 758 字,大约阅读时间需要 2 分钟。

一.知识储备

有符号无符号int取值范围:

有符号int 取值范围[- 2^31 , 2^31-1];
无符号int取值范围[ 0,2^32-1]。

关于中位数:数据排序后,位置在最中间的数值。即将数据分成两部分,一部分大于该数值,一部分小于该数值。中位数的位置:当样本数为奇数时,中位数=(N+1)/2 ; 当样本数为偶数时,中位数为N/2与1+N/2的均值(那么10G个数的中位数,就第5G大的数与第5G+1大的数的均值了)。

思想:

假设整数为无符号数 0-2^32-1;
1.读取10g大小的数字,需要一个64bit的数据空间
  2*5*2^30>2^32, 这里选用64bit;
2.每次读取2g数据进内存,需要分区间段落
  区间数位:2G / 64bit = 256M 个区间。
3.每个区间段落表示的数字范围
32bit的整数最大值为2^32-1,所以区间的范围是2^32 / 256M = 16.
4.遍历10g的数字,找到中位数的区间段
统计每个区间中整数的值。然后从第一个区间的整数值开始累加。当累加到5G时,停止。此时的区间便包含中位数。记下此区间所
表示的范围,设为[a,a+15].并且记下此区间之前所有区间的累加和,设为m。释放掉除包含中位数区间的其他所有区间的内存。
5.再次遍历10g的整数,统计出现在[a,a+15]内整数,有16个数,并且从小到达排序,n0,n1,..n15
6.m+[n0-n15],m加上n0到n15中的数字首次出现大于5g,则a+nx就是所求的中位数。
https://blog.csdn.net/acceptyly/article/details/48240711

都看到这里了,就顺手点击左上角的【关注】按钮,点击右上角的小手,给个评论,关注一下,再走呗!☺

你可能感兴趣的文章
论文笔记:Constructing Narrative Event Evolutionary Graph for Script Event Prediction
查看>>
论文笔记丨Open Hierarchical Relation Extraction
查看>>
论文笔记| BART:Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation
查看>>
【论文笔记】 | Learning to Retrieve Reasoning Paths over Wikipedia Graph for Question Answering
查看>>
论文笔记 | Adversarial Examples for Evaluating Reading Comprehension Systems
查看>>
2021-06-12
查看>>
论文笔记| The Emergence, Advancement and Future of Textual Answer Triggering
查看>>
论文笔记|Open Set Text Classification using Convolutional Neural Networks
查看>>
论文笔记: Hierarchical Chinese Legal event extraction via Pedal Attention Mechanism
查看>>
论文笔记 | Enhancing Pre-Trained Language Representations with Rich Knowledge for MRC
查看>>
论文笔记 | Text Summarization with Pretrained Encoders
查看>>
论文笔记:Document-level Event Extraction via Heterogeneous Graph-based Interaction Model with a Tracker
查看>>
论文笔记丨Inductive Unsupervised Domain Adaptation for Few-Shot Classification via Clustering
查看>>
论文笔记|GSum: A General Framework for Guided Neural Abstractive Summarization
查看>>
论文笔记 | Does Structure Matter? Encoding Documents for Machine Reading Comprehension
查看>>
论文笔记|Self-Supervised Test-Time Learning for Reading Comprehension
查看>>
论文笔记|Open-world Learning and Application to Product Classification
查看>>
论文笔记 _ ELECTRA_ Pre-training Text Encoders as Discriminators Rather than Generators
查看>>
【论文笔记】
查看>>
论文笔记:Exploring Pre-trained Language Models for Event Extraction and Generation
查看>>