HackerRank Find Median(中位数)
2015-12-18 22:52
447 查看
题目链接
Problem Statement
The median of a finite list of numbers can be found by arranging all the integers from lowest to highest value and picking the middle one. For example, the median of{3,3,5,9,11}
is 5.
If there is an even number of integers, then there is no single middle value, and the median is then usually defined to be the mean of the two middle values. For examples, the median of{3,5,7,9}
is (5+7)2=6.
Given that integers are read from a data stream, find the median of elements read so far in an efficient way.
Input Format
The first line of input will contain integer N,
i.e. the number of integers in the data stream.
Each of the next N
lines will contain an integer ai.
Constraints
1≤N≤105
0≤ai≤105
Output Format
Print N
integers, i.e. the median after each of the input. Report it with precision up to10−1.
Sample Input
Sample Output
Explanation
See the sorted list after each input.
Problem Statement
The median of a finite list of numbers can be found by arranging all the integers from lowest to highest value and picking the middle one. For example, the median of{3,3,5,9,11}
is 5.
If there is an even number of integers, then there is no single middle value, and the median is then usually defined to be the mean of the two middle values. For examples, the median of{3,5,7,9}
is (5+7)2=6.
Given that integers are read from a data stream, find the median of elements read so far in an efficient way.
Input Format
The first line of input will contain integer N,
i.e. the number of integers in the data stream.
Each of the next N
lines will contain an integer ai.
Constraints
1≤N≤105
0≤ai≤105
Output Format
Print N
integers, i.e. the median after each of the input. Report it with precision up to10−1.
Sample Input
10 1 2 3 4 5 6 7 8 9 10
Sample Output
1.0 b198 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5
Explanation
See the sorted list after each input.
#include<stdio.h> //双堆求中位数(一个最大堆,一个最小堆) #include<stdlib.h> #define MAX 100001 /* 算法思想: 1、创建两个堆,一个最大堆,一个最小堆,堆的大小至少为给定元素个数的一半,即(size+1)/2,向上取整 2、假设用mid保存中位数,将第一个元素赋值给mid,作为中位数的初始值 3、依次遍历后面的每一个数据,若比mid小,则插入到最大堆中(最大堆中的元素都比mid小),否则插入到最小堆(元素都比mid大) 4、如果最大堆的元素个数与最小堆的元素个数差值大于1,则将mid插入到个数较少的堆中,然后将个数较多的堆的堆顶元素赋值给mid, 同时,删除该堆顶元素 5、重复3,4,直至元素遍历结束 如果两个堆的元素个数相同,则mid即为中位数(因为最大堆中的元素都比mid小,最小堆中的元素都比mid大,且两个队元素个数相同, 则mid刚好处于中间) 否则,元素较多的堆的堆顶与mid求平均值,即为中位数(假设最小堆的元素较多,则最小堆的堆顶和mid这两个元素的左右的元素个数 相同,即堆顶和mid恰为中间位置的数,中位数即为二者的平均值) */ int MaxHeapNum = 0, MinHeapNum = 0; int MaxHeap[MAX / 2], MinHeap[MAX / 2], a[MAX]; float mid; void MaxHeapIns(int num) //MaxHeap Insert { int i, j, temp; MaxHeap[MaxHeapNum] = num; temp = MaxHeap[MaxHeapNum]; i = MaxHeapNum; j = (i - 1) / 2; while (j >= 0) //SiftUp { if (MaxHeap[j] < temp) { MaxHeap[i] = MaxHeap[j]; i = j; if (i == 0) break; //!!重要!!当i=0时,j=(-1)/2等于0,死循环 j = (i - 1) / 2; } else break; } MaxHeap[i] = temp; MaxHeapNum++; } void MinHeapIns(int num) //MinHeap Insert { int i, j, temp; MinHeap[MinHeapNum] = num; temp = MinHeap[MinHeapNum]; i = MinHeapNum; j = (i - 1) / 2; while (j >= 0) //SiftUp { if (MinHeap[j] > temp) { MinHeap[i] = MinHeap[j]; i = j; if (i == 0) break; j = (i - 1) / 2; } else break; } MinHeap[i] = temp; MinHeapNum++; } void MaxHeapDel() //Delete the top of the MaxHeap { int i, j, temp; MaxHeap[0] = MaxHeap[MaxHeapNum - 1]; temp = MaxHeap[0]; i = 0; j = 2 * i + 1; while (j < MaxHeapNum - 1) //SiftDown { if (j + 1 < MaxHeapNum - 1 && MaxHeap[j] < MaxHeap[j + 1]) j++; if (MaxHeap[j] > temp) { MaxHeap[i] = MaxHeap[j]; i = j; j = 2 * i + 1; } else break; } MaxHeap[i] = temp; MaxHeapNum--; } void MinHeapDel() //Delete the top of the MinHeap { int i, j, temp; MinHeap[0] = MinHeap[MinHeapNum - 1]; temp = MinHeap[0]; i = 0; j = 2 * i + 1; while (j < MinHeapNum - 1) //SiftDown { if (j + 1 < MinHeapNum - 1 && MinHeap[j] > MinHeap[j + 1]) j++; if (MinHeap[j] < temp) { MinHeap[i] = MinHeap[j]; i = j; j = 2 * i + 1; } else break; } MinHeap[i] = temp; MinHeapNum--; } int main() { int n, i; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", a + i); if (i == 0) { mid = a[0]*1.0; //中位数初始值 printf("%.1f\n", mid); } else { if (a[i] < mid) { MaxHeapIns(a[i]); } else { MinHeapIns(a[i]); } if (MinHeapNum - MaxHeapNum > 1) { MaxHeapIns(mid); mid = MinHeap[0]; MinHeapDel(); } if (MaxHeapNum - MinHeapNum > 1) { MinHeapIns(mid); mid = MaxHeap[0]; MaxHeapDel(); } if (MinHeapNum != MaxHeapNum) { printf("%.1f\n", (mid + (MinHeapNum > MaxHeapNum ? MinHeap[0] : MaxHeap[0])) / 2.0); } else { printf("%.1f\n", mid); } } } return 0; }
相关文章推荐
- 基于java中stack与heap的区别,java中的垃圾回收机制的相关介绍
- Grow heap (frag case) 堆内存过大的深入解析
- 深入Java虚拟机
- Converting HEAP to MyISAM in SHOW PROCESSLIST
- 内部代码kghasp1发现某一Heap大小为0
- JDK5.0中JVM堆模型、GC垃圾收集详细解析
- tomcat OutOfMemoryError: PermGen space
- 优先堆也可以用链表
- STL学习记录(十四):其它几种算法
- spark 查找 中位数
- java heap out of memory分析
- davlik heap out of memory
- C/C++程序占用内存分析
- 两个等长有序数组求中位数
- [hackerrank]Weekly Challenges - Week 11
- 排序算法(2)-堆排序
- 初识JVM中堆栈的存储
- 【java实战】利用Jmap查看内存泄露的元凶
- 【java实战】利用Jmap查看内存泄露的元凶