queue 优先队列

eg:uva136

题意 丑数是指不能被2 ,3 ,5 以外素数整除的数,把丑数从小到大排列如下: 1 , 2 ,3 ,5 ,6 。。。

    求第1500个丑数;

分析: 最小的丑数为1,让他进入优先队列,then  2x,3x,5x  这些都是丑数,用优先队列保存一下,然后在从队列出来,在循环。

注意:不要把已生成的丑数多次进入优先队列。可以用set保存判断就好了。

 1 #include<iostream>
 2 #include<vector>
 3 #include<queue>
 4 #include<set>
 5 using namespace std;
 6 typedef long long LL;
 7 const int coeff[3]={2,3,5};
 8
 9 int main()
10 {
11     priority_queue<LL,vector<LL>,greater<LL> > pq;
12     set<LL> s;
13     pq.push(1);
14     s.insert(1);
15     for(int i=1; ;i++)
16     {
17         LL x=pq.top(); pq.pop();
18         if(i==1500)
19         {
20             cout << x << endl;
21         }
22         for(int j=0;j<3;j++)
23         {
24             LL x2=coeff[j]*x;
25             if(!s.count(x2)){
26                 s.insert(x2);     pq.push(x2);
27             }
28         }
29      }
30      return 0;
31 }

时间: 2024-08-19 06:40:41

queue 优先队列的相关文章

STL之stack,queue,优先队列

 1.stack,size(),empty(),pop()函数 #include<stack> #include <iostream> using namespace std; //通过push()方法入栈 //通过size()方法求栈中元素的个数 //通过empty()方法判断栈是否为空 //通过pop()求栈中最顶端的元素 void main() { int num; cin >> num; stack<int> mystack; for (; nu

hdoj 1509 Windows Message Queue(优先队列)

Windows Message Queue 点击打开链接 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4309 Accepted Submission(s): 1701 Problem Description Message queue is the basic fundamental of windows system. For eac

【HUOJ】排序(queue 优先队列)

排序 题意:输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整数就是由若干个‘0’组成的,这时这个整数就是0). 你的任务是:对这些分割得到的整数,依从小到大的顺序排序输出. 思路:我先用string a输入,然后每一个ai判断是不是5,如果不是5就放进一个队列里,如果是5就进行一波判断 另外,我WA了一个地方是输入520这种数,我会输出0 20,然后改了一波23333~ 先放上我的代码

hdu 1509 Windows Message Queue (优先队列)

Windows Message QueueTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9202    Accepted Submission(s): 3836 Problem DescriptionMessage queue is the basic fundamental of windows system. For each pr

手工 stack栈 queue队列 priority queue 优先队列 模板

栈stack: 1 struct sta 2 { 3 int sz[100001]; 4 int top() 5 { 6 return sz[top]; 7 } 8 void push(int x){ 9 sz[++top]=x; 10 } 11 void pop(){ 12 if(top>0) 13 top--; 14 } 15 void cl() 16 { 17 top=0; 18 } 19 int size(){ 20 return top; 21 } 22 }stack;

优先队列Priority Queue和堆Heap

对COMP20003中的Priority queue部分进行总结.图片来自于COMP20003 queue队列,顾名思义特点先进先出 priority queue优先队列,出来的顺序按照优先级priority大小,越大(小)的先pop. 普通的方法: Unsorted array: Construct: O(n) Get highest priority: O(n) Sorted array: Construct: O(n2) Get highest priority: O(1) 使用堆heap

优先队列priority_queue的比较函数

STL头文件:#include<queue> 优先队列: 默认从大到小排列:priority_queuee<node>q; 自定义优先级的三种方法: 1.重载操作符: bool operator < (const node &a, const node &b) { return a.value < b.value; // 按照value从大到小排列 } priority_queue<node>q; (const node &a是用引用

C++标准库和标准模板库

C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标准头文件中定义. 在C++开发中,要尽可能地利用标准库完成.这样做的直接好处包括: (1)成本:已经作为标准提供,何苦再花费时间.人力重新开发呢: (2)质量:标准库的都是经过严格测试的,正确性有保证: (3)效率:关于人的效率已经体现在成本中了,关于代码的执行效率要相信实现标准库的大牛们的水平: (4)良好的编程风格:采用行业中普遍的做法进行开发. 一.C++标准库 C++标准库的内容分为10类, 分别是:C1.语

STL学习一:标准模板库理论基础

STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.现然主要出现在C++中,但在被引入C++之前该技术就已经存在了很长的一段时间. STL的从广义上讲分为三类:algorithm(算法).container(容器)和iterator(迭代器),容器和算法通过迭代器可以进行无缝 地连接.几乎所有的代码都采 用了模板类和模板函数的方式,这相比于传统的由函数和类组成的库来说提供了更好的代码重用机会.在C++标准中,STL被组织为下面的13个头文