优先队列(priority)--hdu4006
来源:互联网 发布:百度推广账户换域名 编辑:程序博客网 时间:2024/05/23 19:05
在优先队列中,优先级高的元素先出队列。
标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。
通过自定义operator<操作符来比较元素中的优先级。
在示例3中输出结果为:
优先级 值
9 5
8 2
6 1
2 3
1 4
标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。
优先队列的第一种用法,也是最常用的用法:
priority_queue<int> qi;
通过<操作符可知在整数中元素大的优先级高。
故示例1中输出结果为:9 6 5 3 2
第二种方法:
在示例1中,如果我们要把元素从小到大输出怎么办呢?
这时我们可以传入一个比较函数,使用functional.h函数对象作为比较函数。
priority_queue<int, vector<int>, greater<int> >qi2;
其中
第二个参数为容器类型。
第二个参数为比较函数。
故示例2中输出结果为:2 3 5 6 9
第三种方法:
自定义优先级。
struct node
{
friend bool operator< (node n1, node n2)
{
return n1.priority < n2.priority;
}
int priority;
int value;
};
在该结构中,value为值,priority为优先级。{
friend bool operator< (node n1, node n2)
{
return n1.priority < n2.priority;
}
int priority;
int value;
};
通过自定义operator<操作符来比较元素中的优先级。
在示例3中输出结果为:
优先级 值
9 5
8 2
6 1
2 3
1 4
题目意思。I 代表插入一个数,Q代表输出第K大的数。
#include <iostream>#include <cstdio>#include <algorithm>#include <queue>#include <vector>using namespace std;int main(){ int n,k; while(scanf("%d %d",&n,&k) == 2) { priority_queue<int,vector<int>,greater<int> > Q; for(int i = 1;i <= n;i++) { char opt; scanf(" %c",&opt); if(opt == 'I') { int a; scanf("%d",&a); Q.push(a); if(Q.size() > k) Q.pop(); } else printf("%d\n",Q.top()); } }}
0 0
- 优先队列(priority)--hdu4006
- hdu4006(优先队列)
- hdu4006优先队列
- HDU4006-优先队列
- HDU4006(优先队列)
- 优先队列(Priority Queue)
- 优先队列(priority queue)
- 优先队列(Priority Queue)
- hdu4006 The kth great number(优先队列)
- hdu4006 The kth great number(优先队列)
- hdu4006 The kth great number(优先队列)
- 浅谈优先队列(priority queue)
- Priority Queue(Heaps)--优先队列(堆)
- priority queue 优先队列
- priority queue 优先队列
- 优先队列priority queue
- HDU4006-The kth great number(优先队列)
- [优先队列] HDU4006 The kth great number
- 找零钱
- java基础__IO
- Java并发编程之线程管理(高级线程同步7)
- . Line to Cashier
- Contacts和RawContacts的区别
- 优先队列(priority)--hdu4006
- PL/SQL database character set
- Hbase集群搭建
- linux网络编程八:高效的sendfile,实现零拷贝
- 算法学习(随记)
- Codeforces Round #239 (Div. 2)
- java基础__多线程
- 大规模分布式系统架构与设计实战
- Cocos2d-x笔记(六)—使用TexturePacker对图片进行打包、加密