zoukankan      html  css  js  c++  java
  • Priority Queue

    A priority queue is a data structure which maintains a set SS of elements, each of with an associated value (key), and supports the following operations:

    • insert(S,k)insert(S,k): insert an element kk into the set SS
    • extractMax(S)extractMax(S): remove and return the element of SS with the largest key

    Write a program which performs the insert(S,k)insert(S,k) and extractMax(S)extractMax(S) operations to a priority queue SS. The priority queue manages a set of integers, which are also keys for the priority.

    Input

    Multiple operations to the priority queue SS are given. Each operation is given by "insert kk", "extract" or "end" in a line. Here, kk represents an integer element to be inserted to the priority queue.

    The input ends with "end" operation.

    Output

    For each "extract" operation, print the element extracted from the priority queue SS in a line.

    Constraints

    • The number of operations 2,000,000≤2,000,000
    • 0k2,000,000,0000≤k≤2,000,000,000

    Sample Input 1

    insert 8
    insert 2
    extract
    insert 10
    extract
    insert 11
    extract
    extract
    end
    

    Sample Output 1

    8
    10
    11
    2

    #include <iostream>
    #include <queue>
    #include <string>
    using namespace std;
    
    int main()
    {
    	priority_queue<int> PQ;
    	string com;
    	int n;
    	
    	while(cin >> com)
    	{
    		if(com == "end")
    		{
    			break;
    		}
    		if(com[0] == 'i')
    		{
    			cin >> n;
    			PQ.push(n);
    		}
    		else if(com[0] == 'e')
    		{
    			cout << PQ.top() << endl;
    			PQ.pop();
    		}
    	}
    	
    	return 0;
    } 
    

      

  • 相关阅读:
    Python-函数
    Python-运数符
    Python-条件判断
    Python-变量
    移动端页面布局的那些事儿
    关于ie7下display:inline-block;不支持的解决方案
    SuperSlidev2.1 轮播图片和无缝滚动
    解决xmapp中Apache端口号占用问题
    npm 常用命令详解
    python函数总结
  • 原文地址:https://www.cnblogs.com/mjn1/p/10750783.html
Copyright © 2011-2022 走看看