博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU1509 优先队列
阅读量:6929 次
发布时间:2019-06-27

本文共 1932 字,大约阅读时间需要 6 分钟。

题意(需注意两点):

       1.Note that the less priority value means the higher priority(标准优先队列)

       2.the one comes first will be processed firs(当priority优先级相同是,先进先出)

       

#include
#include
#include
using namespace std;struct node //自定义优先队列结构体{ char str[100]; int par; int pri; int index; bool operator <(const node &x)const//重载 > 号会出错 { if(pri!=x.pri) return pri>x.pri;//小的先进先出 else return index>x.index;//与main中的k对应,先进先出 }};int main(){ priority_queue
que;//优先队列的定义 int k=0; char cmd[4]; node temp; while(cin>>cmd) { if(strcmp(cmd,"GET")==0)//输入GET时 { if(!que.empty()) { temp=que.top(); que.pop(); cout<
<<' '<
<
>temp.str>>temp.par>>temp.pri; temp.index=++k; que.push(temp); } } return 0;}

一起学习一下优先队列相关知识:

/

empty() 如果队列为空返回真

pop() 删除对顶元素

push() 加入一个元素

size() 返回优先队列中拥有的元素个数

top() 返回优先队列对顶元素

在默认的优先队列中,优先级高的先出队。在默认的int型中先出队的为较大的数。

使用方法:

头文件:

#include <queue>

声明方式:

1、普通方法:

priority_queue<int>q;

//通过操作,按照元素从大到小的顺序出队

2、自定义优先级:

struct cmp

{
operator bool ()(int x, int y)
{
return x > y; // x小的优先级高
//也可以写成其他方式,如: return p[x] > p[y];表示p[i]小的优先级高
}
};
priority_queue<int, vector<int>, cmp>q;//定义方法
//其中,第二个参数为容器类型。第三个参数为比较函数。

3、结构体声明方式:

struct node

{
int x, y;
friend bool operator < (node a, node b)
{
return a.x > b.x; //结构体中,x小的优先级高
}
};
priority_queue<node>q;//定义方法
//在该结构中,y为值, x为优先级。
//通过自定义operator<操作符来比较元素中的优先级。
//在重载”<”时,最好不要重载”>”,可能会发生编译错误

///

STL 中队列的使用(queue)

基本操作:

push(x) 将x压入队列的末端

pop() 弹出队列的第一个元素(队顶元素),注意此函数并不返回任何值

front() 返回第一个元素(队顶元素)

back() 返回最后被压入的元素(队尾元素)

empty() 当队列为空时,返回true

size() 返回队列的长度

使用方法:

头文件:

#include <queue>

声明方法:

1、普通声明

queue<int>q;

2、结构体

struct node

{
int x, y;
};
queue<node>q;

转载于:https://www.cnblogs.com/XDJjy/archive/2013/04/02/2996609.html

你可能感兴趣的文章
秋色园QBlog技术原理解析:独创的多语言翻译机制(九)
查看>>
MySQL 5.7在RHEL/CentOS上的安装
查看>>
mysql AB复制搭建以及常见故障排查
查看>>
我对数据库范式的理解
查看>>
IPSEC 、GRE、PIX
查看>>
linux环境开发私房菜
查看>>
iptable和netfilter
查看>>
查看进程端口情况
查看>>
Kirin的茶水间>正文 春Phone计划 51cto沙龙上海站
查看>>
swift UI专项训练29 Page Control页控件
查看>>
Linux目录结构
查看>>
Ghost使用及找不到Ghostree.txt文件问题
查看>>
VMware vSphere常见问题汇总(二十)
查看>>
Wijmo 更优美的jQuery UI部件集:从wijwizard和wijpager开始
查看>>
SQL Server AlwaysOn客户端连接
查看>>
在VC中彻底玩转Excel
查看>>
删除骇客隐藏帐号
查看>>
设计模式之使用Enum来实现strategy(策略模式系列2)
查看>>
photoshop调整图片
查看>>
细节问题:ZEROFILL的用法范围。
查看>>