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

来源:互联网 发布:李东峰 北京航天数据 编辑:程序博客网 时间:2024/06/05 08:58

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 longint 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;}
0 0
原创粉丝点击