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 {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.
The next N lines will contain an integer ai each in order.

Constraints 
1N105 
0ai105

Output Format

Print N integers, i.e. the median after each of the input. Report it with precision up to 101.

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;}


0 0
原创粉丝点击