URAL1306-Sequence Median (priority queue)

Source: Internet
Author: User

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

Input Output
43645
4.5
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()
         
          

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.