URAL1306-Sequence Median (priority queue)
1306. Sequence MedianTime limit: 1.0 second
Memory limit: 1 MB
Language limit: C, C ++, Pascal
Given a sequenceNNonnegative integers. Let's define the median of such sequence. IfNIs odd the median is the element with stands in the middle of the sequence after it is sorted. One may notice that in this case the median has position (N+ 1)/2 in sorted sequence if sequence elements are numbered starting with 1. IfNIs even then the median is the semi-sum of the two "middle" elements of sorted sequence. I. e. semi-sum of the elements in positionsN/2 and (N/2) + 1 of sorted sequence. But original sequence might be unsorted. Your task is to write program to find the median of given sequence. InputThe first line of input contains the only integer numberN-The length of the sequence. sequence itself follows in subsequent lines, one number in a line. the length of the sequence lies in the range from 1 to 250000. each element of the sequence is a positive integer not greater than 231? 1 random Sive. OutputYou shoshould print the value of the median with exactly one digit after decimal point. Sample
Problem Source:II Collegiate Students Urals Programming Contest. yekaterinburg, rjl 3-4, 1998 question: Calculate the median of 25000 numbers, but the memory card is very dead and cannot fully open the array idea: as long as the first 1/2 plus a large number of values are maintained in the priority queue.
#include
#include
#include
#include
#include
#include #include
using namespace std;priority_queue
, greater
> pq;int main(){ int n; while(~scanf("%d",&n)){ int cnt = n/2+1; while(!pq.empty()) pq.pop(); for(int i = 0; i < n; i++){ int d; cin >> d; if(pq.size()