码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4006 The kth great number (优先队列)

时间:2016-08-12 18:17:11      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

题目链接

题意:n次操作,每次可以用 I 表示写入一个数,或者用 Q 表示询问第k大的数是多少。

题解:优先队列,只保留前k大的数。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
int main()
{
    int n,k;
    char s[15];
    while(cin>>n>>k)
    {
        priority_queue<int,vector<int>,greater<int> >q;
        int num;
        while(n--)
        {
            cin>>s;
            if(s[0]==I)
            {
                cin>>num;
                if(q.size()<k)
                {
                    q.push(num);
                }
                else if(q.top()<num)
                {
                    q.pop();
                    q.push(num);
                }
            }
            else
                cout<<q.top()<<endl;
        }
    }
    return 0;
}

 

HDU 4006 The kth great number (优先队列)

标签:

原文地址:http://www.cnblogs.com/Ritchie/p/5765655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!