hdu4006

来源:互联网 发布:万网域名过户 编辑:程序博客网 时间:2024/05/16 06:02

大致题意:给出n个操作,每次选择插入数字或者询问已经给出数字中第k大的数,k给定

测试案例:

input:

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

output:

1
2
3


解题思路:由于这题只需要求一个第k大的数,k已经给定,那么可以建立一个排序树,树中只存前k大的数,当遇到插入操作时,如果插入数小于第k大的数那么对询问没任何影响,不予理会,如果大于第k大的数,那么删除树中最小的数将其插入即可。

代码:

//集合默认排序从小到大#include <iostream>#include <cstdio>#include <cstring>#include <set>using namespace std;int main(){    multiset<int> s;    multiset<int>::iterator it;    int n,k,t;    char ch;    while (scanf("%d%d",&n,&k)==2)    {        s.clear();        int i=0;        while (i<k)        {            scanf("%c",&ch);            if (ch=='I')//先在multiset中存入k个数            {                scanf("%d",&t);                s.insert(t);                i++;            }        }        while(i<n)        {            scanf("%c",&ch);            if (ch=='I')            {                scanf("%d",&t);                it=s.begin();//集合默认排序从小到大                if (t>*it)//插入数大于集合中最小的数                {                    s.erase(it);//删除集合中最小的数                    s.insert(t);                }                i++;            }            else if (ch=='Q')            {                it=s.begin();                printf("%d\n",*it);                i++;            }        }    }    return 0;}





0 0
原创粉丝点击