HDU 4006 The kth great number(STL)

本文介绍了一种利用C++ STL set的数据结构来解决寻找数组中第K大的元素的问题。通过插入和删除操作确保set的大小始终为K,从而在每次查询时都能快速获得当前第K大的元素。

/*用STL set  去维护集合的大小为k*/

Problem : 4006 ( The kth great number )     Judge Status : Accepted
RunId : 6313274    Language : C++    Author : CherryChou
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
#include<iostream>
#include<cstdio>
#include<string.h>
#include<set>
#include<algorithm>
int n,k;
char ch;
using namespace std;
int main(){
    while(cin>>n>>k){
    int x;
    multiset<int> aa;
    while(n--){
    cin>>ch;
    if(ch=='I'){
    scanf("%d",&x);
    aa.insert(x);
    if(aa.size()>k)
    aa.erase(aa.begin());}
    else printf("%d\n",*aa.begin());}}
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值