您的位置:首页 > 其它

优先队列——HDU1873

2015-06-19 11:46 295 查看
优先队列

(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;
}


$(".MathJax").remove();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: