首页 > 其他 > 详细

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

时间:2016-08-12 18:13:56      阅读:104      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!