熟练优先队列声明格式

  • 数据结构实验之排序四:寻找大富翁

  • 学习一下
  • 重载运算符形式
  • struct cmp
    {
    
        bool operator()(Node a, Node b)
        {
    
            if(a.x == b.x) return a.y>b.y;
            return a.x>b.x;
        }
    }
    priority_queue<Node, vector<Node>, cmp>p;
    
  • 本题就是小顶堆维护:
  • #include<bits/stdc++.h>
    using namespace std;
    int n,m,x,cnt;
    vector<int>a;
    int main()
    {
        ios::sync_with_stdio(false);
        priority_queue<int,vector<int>,greater<int> >q;
        cin>>n>>m;
        while(n--)
        {
            cin>>x;
            if(cnt<m)
            {
                q.push(x);
                cnt++;
            }
            else
            {
                if(x>q.top())
                {
                    q.pop();
                    q.push(x);
                }
            }
        }
        while(m--)
        {
            a.push_back(q.top());
            q.pop();
        }
        for(int i=cnt-1; i>=0; i--)
            if(i==0)
                cout<<a[i]<<endl;
            else
                cout<<a[i]<<" ";
        return 0;
    }
    
    
  •  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值