The kth great number

The kth great number
Time Limit : 2000/1000ms (Java/Other) Memory Limit :65768/65768K (Java/Other)
Total Submission(s) : 26 Accepted Submission(s) : 9
Problem Description
Xiao Ming and Xiao Bao are playing asimple Numbers game. In a round Xiao Ming can choose to write downa number, or ask Xiao Bao what the kth great number is. Because thenumber 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 eachtest 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 willwrite 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 integerrepresenting the largest number of islands that all lie on oneline.

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

Sample Output
1
2
3

Hint
Xiao Ming won't ask Xiao Bao the kth great number when the numberof the written number is smaller than k.(1=<k<=n<=1000000).
AC代码
#include<cstdio>
#include <cstdlib>
#include <vector>
#include <queue>
using namespace std;
int main()
{
      int n, k,num; char cmd;
      while (scanf("%d%d", &n, &k) == 2 )
      {
              priority_queue< int,vector<int>,greater<int> >Q;
              for ( int i = 0; i < n; ++i )
              {
                      scanf(" %c", &cmd);
                      if ( cmd == 'I' )
                      {
                              scanf("%d", &num); Q.push(num);
                              if (Q.size() > k) Q.pop();
                      }
                      else if ( cmd == 'Q' )
                      {
                              printf("%d\n", Q.top());
                      }
              }
      }
      return0;
}
总结
关于priority_queue
1,关于STL中的priority_queue:确定用top()查看顶部元素时,该元素是具有最高优先级的一个元素.调用pop()删除之后,将促使下一个元素进入该位置.
2,如同stack和queue,priority_queue是一个基于基本序列容器进行构建的适配器,默认的序列器是vector.

模板原型:

priority_queue<T,Sequence,Compare>

T:存放容器的元素类型

Sequence:实现优先级队列的底层容器,默认是vector<T>

Compare:用于实现优先级的比较函数,默认是functional中的less<T>

priority_queue<int,vector<int>,greater<int>>que;//最小优先队列

priority_queue<int,vector<int>,less<int>>que;//最大优先队列

priority_queue<int>que;//默认为最大优先队列

常用的操作如下:

empty() 如果优先队列为空,则返回真
pop() 删除第一个元素
push() 加入一个元素
size() 返回优先队列中拥有的元素的个数

top()返回优先队列中有最高优先级的元素

 ***priority_queue对于基本类型的使用方法相对简单。
他的模板声明带有三个参数,priority_queue<Type, Container,Functional>
Type 为数据类型, Container 为保存数据的容器,Functional 为元素比较方式。
Container 必须是用数组实现的容器,比如 vector, deque 但不能用 list.
STL里面默认用的是 vector. 比较方式默认用 operator< ,所以如果你把后面俩个
参数缺省的话,优先队列就是大顶堆,队头元素最大。

但是在使用时必须注意:priority_queue放置元素时,不会判断元素是否重复。(因为在模板的第二个参数时顺序容器,不能保证元素的唯一性)此外可以替代默认的Compare函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值