A - 签到

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64u

Description

Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to write down a number, or ask Xiao Bao what the kth great number is. Because the number written by Xiao Ming is too much, Xiao Bao is feeling giddy. Now, try to help Xiao Bao.
 

Input

There are several test cases. For each test case, the first line of input contains two positive integer n, k. Then n lines follow. If Xiao Ming choose to write down a number, there will be an " I" followed by a number that Xiao Ming will write down. If Xiao Ming choose to ask Xiao Bao, there will be a "Q", then you need to output the kth great number.
 

Output

The output consists of one integer representing the largest number of islands that all lie on one line.
 

Sample Input

8 3 I 1 I 2 I 3 Q I 5 Q I 4 Q
 

Sample Output

1 2 3

HintXiao Ming won't ask Xiao Bao the kth great number when the number of the written number is smaller than k. (1=<k<=n<=1000000).
题目大意:

有n步操作,I代表往这一串数里面再加一个,Q则代表查询第k大的数是哪一个。

思路分析:首先要明白第k大的数实际上就是有k-1个数比他大,另外数据范围很大,直接暴力肯定TLE,

因此可以想到使用优先队列这种数据结构,构造最小堆,堆顶的元素刚好是第k大的数,因此当q.size()>k时

直接pop(),避免爆内存。

代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
int main()
{
    int n,k,m;
    char s[5];
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        priority_queue<int,vector<int>,less<int> >Q;
        while(n--)
        {
            scanf("%s",s);
             if(s[0]=='I')
             {
                    scanf("%d",&m);
                    Q.push(m);
                    if(Q.size()>k) Q.pop();
             }
            else
            {
                printf("%d\n",Q.top());
            }
        }
    }
    return 0;
}

hdu4006 优先队列的更多相关文章

  1. HDU 4006 The kth great number 优先队列、平衡树模板题(SBT)

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  2. 堆排序与优先队列——算法导论(7)

    1. 预备知识 (1) 基本概念     如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...

  3. 数据结构:优先队列 基于list实现(python版)

    #!/usr/bin/env python # -*- coding:utf-8 -*- #Author: Minion-Xu #list实现优先队列 class ListPriQueueValueE ...

  4. python优先队列,队列和栈

    打印列表的疑问 class Node: def __str__(self): return "haha" print([Node(),Node()]) print(Node()) ...

  5. 数据结构作业——Sanji(优先队列)

    山治的婚约 Description 我们知道,山治原来是地下有名的杀人家族文斯莫克家族的三子,目前山治的弟弟已经出现,叫做四治,大哥二哥就叫汪(One)治跟突(Two)治好了(跟本剧情无关) .山治知 ...

  6. Java优先队列

    按照Java api的说法: java.util.PriorityQueue.PriorityQueue() Creates a PriorityQueue with the default init ...

  7. 优先队列实现Huffman编码

    首先把所有的字符加入到优先队列,然后每次弹出两个结点,用这两个结点作为左右孩子,构造一个子树,子树的跟结点的权值为左右孩子的权值的和,然后将子树插入到优先队列,重复这个步骤,直到优先队列中只有一个结点 ...

  8. “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)

    题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...

  9. Dijkstra算法优先队列实现与Bellman_Ford队列实现的理解

    /* Dijkstra算法用优先队列来实现,实现了每一条边最多遍历一次. 要知道,我们从队列头部找到的都是到 已经"建好树"的最短距离以及该节点编号, 并由该节点去更新 树根 到其 ...

随机推荐

  1. DHTML【2】--HTML

    通过题目,大家已经明确知道,从这一节开始介绍DHTML中的最基础的部分HTML,对于HTML等概念上一节已经做了概述,这一节不再赘余.在学习HTML之前,先告诉大家一个好消息,HTML不难,比C++. ...

  2. JS中setAttribute的兼容性问题(摘自leejersey)

    class和className兼容方法: object.setAttribute("class","content") 在IE8.Chrome.火狐.Opera ...

  3. 分数相加减的代码(c++)

    #include <iostream> using namespace std; int gy(int a,int k1) {int min; if(a>k1)min=k1; els ...

  4. php生成批次唯一code(转)

    /** * @param int $no_of_codes//定义一个int类型的参数 用来确定生成多少个优惠码 * @param array $exclude_codes_array//定义一个ex ...

  5. DataSet排序

    //排序            if (ds != null && ds.Tables.Count > 0)            {                DataVi ...

  6. ubuntu系统下adb连接手机

    发现Ubuntu12.04不能连接小米开发,adb devices不能看到设备!  搞了一个上午才搞成功! 小米手机利用USB连接到Ubuntu 12.04系统.运行下面的命令: longskywan ...

  7. QT下实现对Linux Shell调用的几种方法

    使用QProcess QThread ============================================ #include <QProcess>int main(){ ...

  8. 使用NUnit进行项目的单元测试

    using System; using System.Collections; using System.Collections.Generic; using NUnit.Framework; nam ...

  9. js深入研究之初始化验证

    <script type="text/javascript"> var Book = function(isbn, title, author) { if(!this. ...

  10. Linux企业级项目实践之网络爬虫(17)——存储页面

    在爬虫系统中数据的流量相当大,要处理的数据内容不仅包括爬虫系统的各种数据结构空间,而且包括从外部节点中得到的各种数据,比如HTTP请求,HTML页面,ROBOT.TXT等等.如果对这些内容处理不当,那 ...