The kth great number multiset应用(找第k大值)

The kth great number

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.

InputThere 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. 
OutputThe 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

Hint

Xiao  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).

开一个multiset(允许存在相同值的集合且加入后自动排序),维护k个元素,多出在头部删除,最后取第一个元素即可,适用于找第几大第几小的题。
#include<stdio.h>
#include<set>
using namespace std;
int main()
{
    int n,k,x,i;
    char c;
    while(~scanf("%d%d",&n,&k)){
        multiset<int> a;
        for(i=1;i<=n;i++){
            getchar();
            scanf("%c",&c);
            if(c==‘I‘){
                scanf("%d",&x);
                a.insert(x);
                if(a.size()>k) a.erase(a.begin());
            }
            else printf("%d\n",*a.begin());
        }
    }
    return 0;
}
时间: 2024-08-30 06:21:10

The kth great number multiset应用(找第k大值)的相关文章

POJ 2104 K-th Number 主席树 区间第K大

今天第一次接触可持久化数据结构,还是有必要总结一下的. 首先对于查找第k大的问题,先搞清楚怎么样通过利用N颗线段树来求解.如果是求全局第K大,那么可以把数字的值作为位置插入线段树,然后通过区间和+二分来找到第k个位置.因为是通过区间和来找第k大的,显然是满足前缀和性质的,所以查询l,r区间的第k打,就直接根据1-l - 1,1-r两个区间建立两颗线段树,然后通过节点依次相减来求得第k大值.所以这样子解需要的内存空间是n*n*logn的(不需要管数的范围,范围再大也可以通过离散化缩小到n). 但是

POJ2985 The k-th Largest Group[树状数组求第k大值 并查集]

The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8807   Accepted: 2875 Description Newman likes playing with cats. He possesses lots of cats in his home. Because the number of cats is really huge, Newman wants to g

九章算法面试题64 找第k大的特殊数

九章算法官网-原文网址 http://www.jiuzhang.com/problem/65/ 题目 有一种特殊的数,它的素数因子只有可能是3,5,7,不可能是其他的素数, 我们把这种数从小到大排序,得到3,5, 7, 9, 15 ... 现在我们要求其中第K大得数是多少,比如其中第4大的数是9. 在线测试本题 http://lintcode.com/en/problem/kth-prime-number/ 解答 我们所要求的元素如果除以3,5,7然后排序过后可以分成为三类元素. a. 1×3,

无序数组中找第k大的数

类快排算法 由于只要求找出第k大的数,没必要将数组中所有值都排序. 快排中的partition算法,返回key在数组中的位置的cnt(相对于left的偏移量),如果cnt正好等于k,那么问题则得到解决:如果cnt小于k,去左边找第k个:如果cnt>k,则去右边找第k-cnt个.直到key的位置等于k-1,则找对问题的解. /*快排中的划分算法*/ int partition(int* input, int low, int high) { int tmp = input[low]; // 取一个

若干个(大量)数字中找前K大/小的元素--数值型

方法一:根据快速排序划分的思想 : (1) 递归对所有数据分成[a,b)b(b,d]两个区间,(b,d]区间内的数都是大于[a,b)区间内的数 : (2) 对(b,d]重复(1)操作,直到最右边的区间个数小于100个. 注意[a,b)区间不用划分 :因为[a,b)区间一定小于(b,d]区间: (3) 返回上一个区间,并返回此区间的数字数目. 如果个数大于100,对(b,d]重复(1)操作,直到最右边的区间个数小于100个: 如果个数小于100,对上一区间的左边进行划分,分为[a2,b2)b2(b

★ 【2008】提高组五1 找第k大的数 C++版

快排找第k大模板

1 int get_kth(int l,int r) 2 { 3 if (l==r) 4 return a[r]; 5 int i=l,j=r,mid=a[(l+r)>>1]; 6 while (i<j) 7 { 8 while (a[i]<mid) 9 i++; 10 while (a[j]>mid) 11 j--; 12 if (i<j) 13 { 14 swap(a[i],a[j]); 15 i++; 16 j--; 17 } 18 } 19 if (k<=

215. Kth Largest Element in an Array 第K大的数

class Solution { public: int quicksort(vector<int>& nums, int start, int end, int k){ int i = start; int j = end; int x = nums[i]; while (i<j){ //快排核心- while (nums[j]<x && i<j) j--; if (i<j) nums[i++] = nums[j]; while (nums[i

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

The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) [Problem Description] Xiao  Ming  and  Xiao  Bao  are  playing  a  simple  Numbers  game.  In  a  round  Xiao  Ming  can choose  to  write  down