The kth great number(小根堆思想,模板题)

简介: The kth great number(小根堆思想,模板题)

Problem 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


题目就是要求我们计算第k大的数,所以我们可以用小根堆的思想;


我们一直保持堆里面由有k个数,那么第k大的数就是堆顶,且一直更新堆,最后更新所有的数之后,我们就可以把堆顶输出 ,就是答案。


听懂了记得给个赞鼓励一下,码字不易,用爱发电。


上ac代码。

f58230e9f063709cf3167704f4efdf14.gif


有事你就q我;QQ2917366383


学习算法

 

  #include<cstdio>
  #include<cstdlib>
  #include<cstring>
  #include<queue>
  #include<iostream>
  #include<algorithm>
  using namespace std;
  int main()
  {
    int n,k,a;
    while(scanf("%d%d",&n,&k)!=EOF)//多组数据输入不影响 
    {
      priority_queue<int ,vector<int>,greater<int> >q; //小根堆,小到大 
      int c=0;//标记堆里面元素个数 
      for(int i=0;i<n;i++)
      {
        char b;
        cin>>b;//输入判断的字符 
        if(b=='I')
        {
          if(c<k)
          {
            scanf("%d",&a);//输入数字 
            c++;
            q.push(a);//放进堆里面 
          }
          else if(c>=k)//如果超过或者等于第k个数 
          {
            scanf("%d",&a);//输入数字
            if(a>q.top())//比较后面输入的数是不是比前面第一个大, 
            {
              q.pop();//把第一个删掉
              q.push(a);//后面的数字放进 堆里面
            }
          }
        }
        else if(b=='Q')//结束条件 
        {
          printf("%d\n",q.top());//输出第一个 
        }
      }   
    } 
  }
相关文章
|
7月前
codeforces 285C - Building Permutation
题目大意是有一个含n个数的数组,你可以通过+1或者-1的操作使得其中的数是1--n中的数,且没有重复的数。 既然是这样的题意,那么我就应该把原数组中的数尽量往他最接近1--n中的位置放,然后求差绝对值之和,但有多个数,怎么使他们和最小,这样就要对其进行排序了,直接按大小给它们安排好位置,然后计算。
19 0
|
7月前
|
开发框架 .NET
poj 3468 A Simple Problem with Integers线段树区间修改
题目意思很简单,有N个数,Q个操作, Q l r 表示查询从l到r 的和,C l r v 表示将从l到r 的值加上v,明显的线段树,不知道线段树的人肯定暴力,肯定超时,哈哈!!
19 0
|
9月前
UVa10776 - Determine The Combination(有重复元素的组合问题)
UVa10776 - Determine The Combination(有重复元素的组合问题)
29 0
|
机器学习/深度学习
AtCoder Beginner Contest 215 F - Dist Max 2 (二分 单调队列)
AtCoder Beginner Contest 215 F - Dist Max 2 (二分 单调队列)
106 0
AtCoder Beginner Contest 214 D.Sum of Maximum Weights (思维 并查集)
AtCoder Beginner Contest 214 D.Sum of Maximum Weights (思维 并查集)
89 0
|
人工智能 BI
AtCoder Beginner Contest 216 F - Max Sum Counting (降维 背包dp)
AtCoder Beginner Contest 216 F - Max Sum Counting (降维 背包dp)
99 0
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
105 0
AtCoder Beginner Contest 223 D - Restricted Permutation(建图 思维 构造 拓扑排序)
AtCoder Beginner Contest 223 D - Restricted Permutation(建图 思维 构造 拓扑排序)
88 0
|
网络架构
Codeforces Round #755 D. Guess the Permutation(交互 二分)
Codeforces Round #755 D. Guess the Permutation(交互 二分)
70 0
UCF 2021 Practice F.Balanced Strings (思维 组合数学)
UCF 2021 Practice F.Balanced Strings (思维 组合数学)
80 0