优先队列的详解与应用
2015-08-17 21:35
465 查看
优先队列的定义:优先队列是0个或多个元素的集合,每个元素都有一个优先权或值,对优先队列执行的操作有1) 查找;2) 插入一个新元素;3) 删除.在最小优先队列(min priority queue)中,查找操作用来搜索优先权最小的元素,删除操作用来删除该元素;对于最大优先队列(max
priority queue),查找操作用来搜索优先权最大的元素,删除操作用来删除该元素.优先权队列中的元素可以有相同的优先权,查找与删除操作可根据任意优先权进行.
个人认为,优先队列是一种特殊的队列,每个元素被赋予优先权,当访问队列元素师,具有最高优先权的元素先被访问。也就是largest-in,first-out。
优先队列的时间复杂度为O(logn);
首先附上百度百科的优先队列类定义:
后来自己在网上看了一些大神的博客,仿照人家的代码练习了一些,贴上代码:
最后,附上一道题目,一开始做这题一直TML,后来看到大神用了优先队列的方法,觉得很巧妙(是我太无知,当时还不会用优先队列= =!)
时间限制(C/C++):1000MS/3000MS 运行内存限制:65536KByte
总提交:109 测试通过:29
描述
给定m以及n个数,这n个数每个数字的大小都在[0, 100000]之间。你需要输出每m个数中的最大数。即1到m中的最大数,2到m+1中的最大数......一直到n-m+1到n中的最大数。比如对于m=3,n=6,六个数字为1 2 3 3 4 1时,前三个数字1 2 3最大是3,然后接下来三个数字2 3 3最大是3,接下来3 3 4最大是4,接下来3 4 1最大是4。所以需要输出3
3 4 4。
输入
输入数据有多组。每组数据第一行是m和n,2<=m<=n<=7000.接下来一行有n个数字,每个数字范围在[0,100000]之间。m=n=0输入结束。
输出
对于每组数据,输出n-m+1个数字,代表每m个数字之间的最大数。输出见样例,注意每个输出的数字后都有一个空格。
样例输入
样例输出
题解:
priority queue),查找操作用来搜索优先权最大的元素,删除操作用来删除该元素.优先权队列中的元素可以有相同的优先权,查找与删除操作可根据任意优先权进行.
个人认为,优先队列是一种特殊的队列,每个元素被赋予优先权,当访问队列元素师,具有最高优先权的元素先被访问。也就是largest-in,first-out。
优先队列的时间复杂度为O(logn);
首先附上百度百科的优先队列类定义:
#include<iostream> #include<stdlib.h> const int maxPQSize = 50;//缺省元素个数 template<classType>classPQueue { public: PQueue(); ~PQueue() { delete[]pqelements; } voidPQInsert(constType&item); TypePQRemove(); voidmakeEmpty() { count = 0; } intIsEmpty()const { returncount == 0; } intIsFull()const { returncount == maxPQSize; } intLength()const { returncount; } private: Type*pqelements;//存放数组 int count;//队列元素计数 }
后来自己在网上看了一些大神的博客,仿照人家的代码练习了一些,贴上代码:
#include<stdio.h> #include<iostream> #include<string> #include<string.h> #include<algorithm> #include<vector> #include<time.h> #include<queue> #include<stack> #include<iterator> #include<math.h> #include<stdlib.h> #include<limits.h> #include<map> #include<memory.h> //#define ONLINE_JUDGE #define eps 1e-8 #define INF 0x7fffffff //INT_MAX #define inf 0x3f3f3f3f //int?????????????????? #define FOR(i,a) for((i)=0;i<(a);(i)++) //[i,a); #define MEM(a) (memset((a),0,sizeof(a))) #define sfs(a) scanf("%s",a) #define sf(a) scanf("%d",&a) #define sfI(a) scanf("%I64d",&a) #define pf(a) printf("%d\n",a) #define pfI(a) printf("%I64d\n",a) #define pfs(a) printf("%s\n",a) #define sfd(a,b) scanf("%d%d",&a,&b) #define sft(a,b,c)scanf("%d%d%d",&a,&b,&c) #define for1(i,a,b) for(int i=(a);i<b;i++) #define for2(i,a,b) for(int i=(a);i<=b;i++) #define for3(i,a,b)for(int i=(b);i>=a;i--) #define MEM1(a) memset(a,0,sizeof(a)) #define MEM2(a) memset(a,-1,sizeof(a)) #define LL __int64 const double PI = acos(-1.0); template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template<class T> inline T Min(T a, T b) { return a < b ? a : b; } template<class T> inline T Max(T a, T b) { return a > b ? a : b; } using namespace std; template<class T> T Mint(T a, T b, T c) { if (a > b) { if (c > b) return b; return c; } if (c > a) return a; return c; } template<class T> T Maxt(T a, T b, T c) { if (a > b) { if (c > a) return c; return a; } else if (c > b) return c; return b; } struct cmp1{ bool operator()(int &a,int &b){ return a>b; } }; struct cmp2{ bool operator()(int &a,int &b){ return a<b; } }; struct num1{ int x; bool operator<(const num1 &a) const{ return x>a.x; } }; struct num2{ int x; bool operator<(const num2 &a) const{ return x<a.x; } }; int a[]={14,10,56,7,83,22,36,91,3,47,72,0}; num1 nu1[]={14,10,56,7,83,22,36,91,3,47,72,0}; num2 nu2[]={14,10,56,7,83,22,36,91,3,47,72,0}; int main() { #ifndef ONLINE_JUDGE // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); #endif priority_queue<int>que; priority_queue<int,vector<int>,cmp1>que1; priority_queue<int,vector<int>,cmp2>que2; priority_queue<int,vector<int>,greater<int> >que3; priority_queue<int,vector<int>,less<int> >que4; priority_queue<num1>que5; priority_queue<num2>que6; for(int i=0;a[i];i++){ que.push(a[i]); que1.push(a[i]); que2.push(a[i]); que3.push(a[i]); que4.push(a[i]); } for(int i=0;nu1[i].x;i++) que5.push(nu1[i]); for(int i=0;nu2[i].x;i++) que6.push(nu2[i]); printf("采用结构体自定义优先级方式一:\n(priority_queue<int,vector<int>,cmp>que)\n"); printf("Queue 1:\n"); while(!que1.empty()){ printf("%3d",que1.top()); que1.pop(); } putchar(10); printf("Queue 2:\n"); while(!que2.empty()){ printf("%3d",que2.top()); que2.pop(); } putchar(10); printf("采用头文件\"functional\"内定义优先级:\n(priority_queue<int,vector<int>,greater<int>/less<int>>que;)\n"); printf("Queue 3:\n"); while(!que3.empty()){ printf("%3d",que3.top()); que3.pop(); } putchar(10); printf("Queue 4:\n"); while(!que4.empty()){ printf("%3d",que4.top()); que4.pop(); } putchar(10); printf("采用结构体自定义优先级方式二:\n(priority_queue<number>que)\n"); printf("Queue 5:\n"); while(!que5.empty()){ printf("%3d",que5.top()); que5.pop(); } putchar(10); printf("Queue 6:\n"); while(!que6.empty()){ printf("%3d",que6.top()); que6.pop(); } putchar(10); return 0; }
最后,附上一道题目,一开始做这题一直TML,后来看到大神用了优先队列的方法,觉得很巧妙(是我太无知,当时还不会用优先队列= =!)
输出数字[蓝]
时间限制(C/C++):1000MS/3000MS 运行内存限制:65536KByte总提交:109 测试通过:29
描述
给定m以及n个数,这n个数每个数字的大小都在[0, 100000]之间。你需要输出每m个数中的最大数。即1到m中的最大数,2到m+1中的最大数......一直到n-m+1到n中的最大数。比如对于m=3,n=6,六个数字为1 2 3 3 4 1时,前三个数字1 2 3最大是3,然后接下来三个数字2 3 3最大是3,接下来3 3 4最大是4,接下来3 4 1最大是4。所以需要输出3
3 4 4。
输入
输入数据有多组。每组数据第一行是m和n,2<=m<=n<=7000.接下来一行有n个数字,每个数字范围在[0,100000]之间。m=n=0输入结束。
输出
对于每组数据,输出n-m+1个数字,代表每m个数字之间的最大数。输出见样例,注意每个输出的数字后都有一个空格。
样例输入
3 6 1 2 3 3 4 1 3 10 10 11 10 0 0 0 1 2 3 2 0 0
样例输出
3 3 4 4 11 11 10 0 1 2 3 3
题解:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; struct node { int x; int id; bool operator<(const node &a)const { return x<a.x; } }tmp; int main() { int n,m; while(~scanf("%d%d",&n,&m)) { if(m==0&&n==0) break; priority_queue<node> q; for(int i=0;i<n;i++) { scanf("%d",&tmp.x); tmp.id=i; q.push(tmp); } tmp=q.top(); cout<<tmp.x<<" "; for(int i=n;i<m;i++) { scanf("%d",&tmp.x); tmp.id=i; while(q.top().id<=i-n) q.pop(); q.push(tmp); tmp=q.top(); cout<<tmp.x<<" "; } putchar(10); } return 0; }
相关文章推荐
- Hive桶表
- c#通过app.manifest使程序 右键 以管理员身份运行
- objective-c类目Category属性的实现方法
- 详细整理:UITableView优化技巧
- Spring MVC 教程,快速入门,深入分析
- POJ 3321 Apple Tree(树状数组)
- Audiophobia
- innodb_flush_log_at_trx_commit
- java 之 语言基础
- 操作系统 时间片轮转调度算法
- 开源服务发现项目Zookeeper,Doozer,Etcd
- 面向对象的方式进行数据交换网络之间的差异
- 【转】将网页转换为PDF文件?用浏览器或在线工具轻松搞定
- 约瑟夫问题
- uva 503 - Parallelepiped walk(几何)
- 【Warshall_Floyd】
- 蜘蛛侠
- LeetCode (8) String to Integer (atoi) C语言程序
- android 手机屏幕横屏竖屏切换
- 重视不确定性