您的位置:首页 > 其它

紫书章五例题七 丑数 UVa 136(多种方法,在这里介绍优先级队列)

2017-04-03 16:52 218 查看
priority_queue pq;

其实优先级队列为vector,然后vector里的排序是从小到大,然后pq出队的操作对应的是vector的尾删,所以我们看到的pq的出队的数据是从大到小,大数据优先级大。

因为这里的出队元素并不是最先进队的,所以出队方式由queue 的front()变为了top()

如果要改变其排列顺序的话,和sort函数一样,要对其< 进行重载

然后priority_queue

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <set>
using namespace std;
#define LL long long
int a[]={2,3,5};
int main()
{
priority_queue<LL,vector<LL> ,greater<LL> > pq;
set<LL> s;
pq.push(1);
s.insert(1);
for(int i=1;;i++)
{
LL x=pq.top();
pq.pop();
if(i == 1500)
{
printf("The 1500'th ugly number is %d.\n",x);
break;
}
for(int j=0;j<3;j++){
LL x2=x* a[j];
if(!s.count(x2)) {
s.insert(x2);
pq.push(x2);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: