Ural 1306. Sequence median (usage of priority_queue in priority queue)

Source: Internet
Author: User

The recent Ural questions are always a variety of errors. reading and understanding the questions report is something you have never learned. It is a bit hard to understand. However, the quality of Ural questions is quite good, abuse is beneficial to health.

This question is to use an array to directly exceed the memory, and use a priority queue. I just got into touch with this and learned the priority queue.

C ++ STL header file declaration <queue>, <queue> includes queue and priority_queue, and priority_queue is the priority queue. By default, the vector container is used. The priority queue container always puts the elements with the highest priority at the forefront of the queue to keep the queue in order. If one push is 1, 2, 3, 4, 5, 6, the priority queue stores 6, 5, 4, 3, 2, 1.

Priority_queue supports the following operations: Q. Empty (), Q. Size (), Q. Pop (0), Q. Top (), Q. Push (item ).

Usage reference http://www.cplusplus.com/reference/queue/priority_queue/

 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <queue>                        //for priority_queue 5 using namespace std; 6 int main() 7 { 8     priority_queue<unsigned int> ipq; 9     int n;10     scanf("%d", &n);11     unsigned int t;12     for (int i = 1; i <= n / 2 + 1; ++i)13     {14         scanf("%u", &t);15         ipq.push(t);16     }17     int tn = n / 2 + 2;18     while (tn <= n)19     {20         scanf("%u", &t);21         ipq.push(t);22         ++tn;23         ipq.pop();24     }25     if (n & 1)                             //if n is odd26         printf("%u.0\n", ipq.top());27     else28     {29         int t1 = ipq.top();30         ipq.pop();31         printf("%.1f", (double)(t1 + ipq.top()) / 2);32     }33     return 0;34 }

Note the following two points: 1. The result format. If the median is an integer, it must be output. 0, keeping a decimal number. 2 Data Type. Although each number is smaller than 2 ^ 31-1, two numbers in the middle may be int_max, for example, if n = 4 and the four numbers are both 2 ^ 31-1, the sum of the two numbers in the middle will exceed int_max output-1.

The idea of the program is: first add the first n/2 + 1 elements to the priority queue. At this time, the priority queue is in descending order. After each element is added, the largest element of the first element is deleted, when all elements are added to the queue, as the maximum element is deleted, the last element is the median. (When n is an odd and even number, respectively)

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.