Module: Priority queue: priority_queue


Problem

1 /3


Finding the Minimum Using Priority Queue

Theory Click to read/hide

Priority Queue is a container type , which allows you to achieve a constant access speed to the maximum (or minimum) element (O(1)), by increasing the speed of inserting elements into the container to logarithmic (O(logn)).

Basic Operators:


top   -  Accessing the top 
element
 
size  - Returns the number of elements in the container 

push  - inserts an element and sorts the underlying container 

pop  - removes the first element 

Example of getting the maximum:

 
 
#include <iostream>
#include <queue>
#include "stdio.h"
using namespace std;

main()
{

priority_queue <int>p_queue;
int N,a;
scanf("%d",&N);

for(int i = 0;i<N;i++)
{
   
scanf("%d",&a);
p_queue.push(a);

  }
  cout<<p_queue.top();

}

Problem

Given a sequence of numbers. Find the smallest number in it.
 
Input
The number N is given first (the number of numbers in the sequence, 1<=N< ;=100000) and then
N numbers.
 
Output
Print the smallest number.

Enter Output
7
4 2 5 -1 4 6 2
-1