Find Median
来源:互联网 发布:心动网络用户中心在哪 编辑:程序博客网 时间:2024/05/03 10:32
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
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
The next
Constraints
Output Format
Print
Sample Input
1012345678910
Sample Output
1.01.52.02.53.03.54.04.55.05.5
Explanation
See the sorted list after each input.
解题思路:用一个最大堆维护序列中位于前半段位置处的数,采用一个最小堆维护序列中后半段位置处的数,不断更新这两个堆,然后在O(logn)的时间内求解出当前的中位数,总的时间复杂度为O(logn).
代码1:
#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <vector>#include <queue>#include <iostream>#include <algorithm>using namespace std;const int maxn = 100010;priority_queue<int, vector<int>, less<int> > pq1;priority_queue<int, vector<int>, greater<int> > pq2;void init() { while(!pq1.empty()) pq1.pop(); while(!pq2.empty()) pq2.pop();}int get(int x) { if(x % 2 == 0) return x / 2 + 1; return (x + 1) / 2;}int main() { int n, x; init(); scanf("%d", &n); for(int i = 1; i <= n; ++i) { scanf("%d", &x); if((int)pq1.size() < get(i)) { pq2.push(x); int t = pq2.top(); pq2.pop(); pq1.push(t); } else { if(pq1.top() > x) { pq1.push(x); int t = pq1.top(); pq1.pop(); pq2.push(t); } else { pq2.push(x); } } if(i % 2) { printf("%.1lf\n", pq1.top() * 1.0); } else { int t = pq1.top(); pq1.pop(); printf("%.1lf\n", (t + pq1.top())/2.0); pq1.push(t); } } return 0;}
代码2:
#include <cstdio>#include <vector>#include <queue>#include <functional>using namespace std;int main() { priority_queue<int, vector<int>, greater<int> > minQ; priority_queue<int> maxQ; int N, a; scanf("%d", &N); while (N--) { scanf("%d", &a); maxQ.push(a); if (maxQ.size() > minQ.size() + 1) minQ.push(maxQ.top()), maxQ.pop(); else if (!minQ.empty() && maxQ.top() > minQ.top()) { maxQ.push(minQ.top()), minQ.pop(); minQ.push(maxQ.top()), maxQ.pop(); } if (maxQ.size() > minQ.size()) printf("%d.0\n", maxQ.top()); else { a = maxQ.top() + minQ.top(); printf("%d.%d\n", a / 2, (a & 1) ? 5 : 0); } } return 0;}
- Find Median
- Find Median
- Find Median With Two heaps
- maxHeap & minHeap to find median
- Find Median from Data Stream
- Find Median from Data Stream
- Find Median from Data Stream
- HackerRank Find Median(中位数)
- Find Median from Data Stream
- Find Median from Data Stream
- Find Median from Data Stream
- Find Median from Data Stream
- Find Median from Data Stream leetcode 295
- LeetCode Find Median from Data Stream
- leetcode 295:Find Median from Data Stream
- [leetcode295]Find Median from Data Stream
- leetcode 295 : Find Median from Data Stream
- [LeetCode 295] Find Median from Data Stream
- ceph存储 状态机思路在程序设计中的应用
- C++ string类型和char类型转换
- uboot relocation原理详细分析
- 链接和图片
- 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法!
- Find Median
- 初探coreText
- 最大类间方差法OTSU,1979年前后景区分法
- apt-get update ,upgarde 和dist-upgrade
- ABAP模块P类型详细解释
- shell中trap捕获信号
- 文章标题
- AsyncHttpClient异步请求工具箱/简单使用(异步的同步支持)
- Dialog的创建过程