(4006)HDU

本文详细介绍了如何使用C++ STL中的set和优先队列解决动态集合问题,包括插入、删除和查询操作,以及使用迭代器进行元素的管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<cstdio>
#include<string.h>
#include<string>
#include<stack>
#include<set>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<queue>


#define ll __int64
#define lll unsigned long long
#define MAX 1000009
#define eps 1e-8
#define INF 0xfffffff


using namespace std;


int main()//方法1,用可重集set
{
    int n,m,k;
    string ss;
    int i,j;
    int flag;
    while(~scanf("%d%d",&n,&k))
    {
        dp.clear();
        multiset<int>dp;//可重集
        flag = 0;
        for(i = 0; i<n; i++)
        {
            cin>>ss;
            if(ss[0]=='I')
            {
                scanf("%d",&m);
                dp.insert(m);
                multiset<int>::iterator it;
                while(dp.size()>k)
                {
                    it = dp.begin();
                    dp.erase(it);
                }
            }
            if(ss[0]=='Q')
            {
                multiset<int>::iterator it;
                it=dp.begin();
                printf("%d\n",*it);
            }
        }
    }
    return 0;
}
struct cmp
{
    bool operator()(const int &a,const int &b)
    {
        return a>b;
    }
};
int main()//方法2 优先队列 因为队列无法排序,所以用优先队列,
{
    int n,m,k;
    string ss;
    int i,j;
    int flag;


    while(~scanf("%d%d",&n,&k))
    {
        priority_queue<int,vector<int>,cmp> op;
        while(n--)
        {
            cin>>ss;
            if(ss[0]=='I')
            {
                scanf("%d",&m);
                op.push(m);
                while(op.size()>k)
                {
                    op.pop();
                }
            }
            if(ss[0]=='Q')
            {
                printf("%d\n",op.top());
            }
        }
    }
    return 0;

}

呜呜。。STL果真学的不好,墨迹了好久才写出来,刚开始用set的想法是对的,可是不会用迭代器,现学的==

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值