优先队列——HDU1873

优先队列

(1)  priority_queue<int>q;

          //按照元素从大到小的顺序出队

(2) 自定义优先级

      定义方法:

      priority_queue<int,vector<int>,cmp>q;

      //第二个参数为容器类型,第三个为比较函数

 struct cmp

{

       bool   operator()(stu a,sut b)

       {

           return  a.yxj<b.yxj;          //通过定义operator< 操作符来比较元素中的优先级

       }

};

例题:HDU1873


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<stdlib.h>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
char s[10];
int t,a,b;
struct stu
{
    int num,yxj;
};

struct cmp
{
    bool operator()(stu a,stu b)
    {
        if(a.yxj!=b.yxj)
            return a.yxj<b.yxj;  //yxj大的先出队列
            return a.num>b.num;
    }
};
priority_queue<stu,vector<stu>,cmp>q[4];
int main()
{
    while(scanf("%d",&t)!=EOF)
    {
        for(int i=0;i<4;i++)
        {
            while(!q[i].empty())
                q[i].pop();
        }
        stu cur;
        int k=1;
        while(t--)
        {
            scanf("%s",s);
            if(strcmp(s,"IN")==0)
            {
                scanf("%d%d",&a,&b);
                cur.yxj=b;
                cur.num=k;
                q[a].push(cur);
                k++;
            }
            else if(strcmp(s,"OUT")==0)
            {
                scanf("%d",&a);
                if(q[a].empty())
                    printf("EMPTY\n");
                else
                {
                    cur=q[a].top();
                    q[a].pop();
                    printf("%d\n",cur.num);
                }
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值