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
- hdu4006
- hdu4006
- HDU4006
- HDU4006
- hdu4006
- hdu4006优先队列
- hdu4006(优先队列)
- HDU4006-优先队列
- HDU4006(优先队列)
- HDU4006 The kth great number
- HDU4006--The kth great number
- HDU4006(The kth great number)
- 优先队列(priority)--hdu4006
- 【HDU4006】The kth great number
- hdu4006: The kth great number
- HDU4006 The kth great number
- hdu4006 The kth great number
- HDU4006-The kth great number(优先队列)
- dot production
- 函数指针的一种用法
- C语言基础学习教程
- According to TLD or attribute directive in tag file, attribute value does not accept any expressions
- HDU 5441(并查集)
- hdu4006
- 解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variabl
- XENSERVER的IPTABLES
- leetcode_c++:Roman to Integer(013)
- SpringMVC项目中常见问题以及解决方案
- Filter的用法
- hdu 2588
- iOS 数据持久化存储
- Maven使用过程中遇到的问题及解决方案