UVa 136 Ugly Numbers【优先队列】

题意:给出丑数的定义,不能被除2,3,5以外的素数整除的的数称为丑数。

和杭电的那一题丑数一样--这里学的紫书上的用优先队列来做。

用已知的丑数去生成新的丑数,利用优先队列的能够每次取出当前最小的丑数再去生成新的丑数====

大概这儿的优先队列就充当了dp转移方程里面的那个min的意思@_@

 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<cstring>  
 4 #include<algorithm> 
 5 #include<queue>
 6 #include<set>
 7 #include<vector> 
 8 using namespace std;
 9 
10 typedef long long LL;
11 priority_queue<LL,vector<LL>,greater<LL> >pq;
12 set<LL>s;
13 
14 int dir[3]={2,3,5};
15 
16 int main()
17 {
18     LL x;
19     pq.push(1);
20     s.insert(1);
21     for(int i=1;i<=1500;i++)
22     {
23         x=pq.top();pq.pop();//将x的值取出,并将其从当前队列中删除 ,以免下次从优先队列中取最小的数时又把它取出来 
24         if(i==1500)
25         cout<< "The 1500'th ugly number is " <<x<< ".
";
26         for(int j=0;j<3;j++)
27         {
28           LL xx=x*dir[j];
29           if(!s.count(xx))  {pq.push(xx);s.insert(xx);}//s.cout(xx)判断xx是否已经存在 
30         }
31     }
32     return 0;
33 }
View Code
原文地址:https://www.cnblogs.com/wuyuewoniu/p/4297015.html