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;
}

最新文章

  1. php根据地址的经纬度查询周围的城市例子
  2. C#实现自动单击
  3. 图片轮播 js代码
  4. UVaLive 6862 Triples (数学+分类讨论)
  5. 【转】一个安全测试的CheckList
  6. eclipse下切换svn用户
  7. 辛星解读mysql的用户管理
  8. 操作3 mongodb和mysql 开启慢查询日志 ,以及mongodb从配置文件启动
  9. hadoop端口配置指南
  10. Nuget 学习三
  11. Logstash使用grok过滤nginx日志(二)
  12. Graphviz--图形绘制工具
  13. 【Android Studio安装部署系列】三十、从Android studio2.2.2升级到Android studio3.0之路
  14. 2019OO第二单元总结
  15. Sql server 编写99乘法表
  16. spring ----&gt; aop测试需要的Maven依赖/测试时发生的一个exception
  17. UVa LA 3213 - Ancient Cipher 水题 难度: 0
  18. 请教Mysql如何删除 不包含 某些字符的记录
  19. GCD (Grand Central Dispatch) 笔记
  20. SaltStack 批量管理任务计划

热门文章

  1. poj3254状压DP入门
  2. C#使用log4net
  3. Java实现生产者消费者问题与读者写者问题
  4. SSH-key密钥生成
  5. Activiti工作流学习-----基于5.19.0版本(2)
  6. UI控件自定义tableView的分割线的样式
  7. UIApplication详解再解-备
  8. [UVALive] 6492 Welcome Party(最小点覆盖)
  9. Chrome下的语音控制框架MyVoix.js使用篇(一)
  10. TestNG使用Eclipse建立Test Case - 就是爱Java