STL--queue
queue-概述:
队列中没有元素时,称为空队列。
|
bool empty() |
队列为空返回true,否则返回false |
|
void pop() |
删除队列的一个元素 |
|
void push( const TYPE &val ) |
将val元素加入队列 |
|
size_type size() |
返当前队列中的元素数目 |
|
TYPE &back() |
返回一个引用,指向队列的最后一个元素 |
|
TYPE &front() |
返回队列第一个元素的引用 |
|
bool empty() |
优先队列为空返回true,否则返回false |
|
void pop() |
删除优先队列中的第一个元素 |
|
void push( const TYPE &val ) |
添加一个元素到优先队列中,值为val |
|
size_type size() |
返当前队列中的元素数目 |
|
TYPE &top () |
返回一个引用,指向最高优先级的元素 |
#include<iostream>
#include<queue>
#include<set>
#include<vector>
using namespace std; void parse(vector<set<int> >&adj, unsigned int p=)//十分巧妙地递归读入。
{
unsigned int x;
cin>>x;
if(p)
{
adj[p].insert(x);
adj[x].insert(p);
}
while(true)
{
char ch;
cin>>ch;
if(ch==')') break;
parse(adj, x);
}
return;
} int main()
{
//freopen( "in.txt", "r", stdin );
//freopen( "out.txt", "w", stdout );
char ch;
while(cin>>ch)
{
vector<set<int> > adj(, set<int>());
parse(adj);
priority_queue<int, vector<int>, greater<int> >leafs;
int n = ;
for(unsigned int i = ; i<adj.size();i++)
if(adj[i].size())
{
n++;
if(adj[i].size()==)
leafs.push(i);
}
for(int k=; k<n; k++)
{
unsigned int x = leafs.top();
leafs.pop();
unsigned int p = *(adj[x].begin());
if(k>)
cout<<" ";
cout<<p;
adj[p].erase(x);
if(adj[p].size()==)
leafs.push(p);
}
cout<<endl;
}
return ;
}
#include<algorithm>
#include<vector>
#include<queue>
#include<cstdio>
#include<iostream>
using namespace std; int main()
{
int n, k;
while(~scanf("%d%d", &n, &k))
{
priority_queue<int, vector<int>, greater<int> >que;
while(n--)
{
char op[];
scanf("%s", op);
if(op[]=='I')
{
int val;
scanf("%d", &val);
que.push(val);
while(que.size()>k) que.pop();
}
else printf("%d\n", que.top());
}
}
return ;
}
(提醒:不要直接交代码, 上面代码不能通过杭电的编译器)。
然后稍加改变, 编写自己的比较函数,而不是用自带的算子(greater)。 结果就过啦! 也是十分的蛋疼,十分的无语!!!。
#include<algorithm>
#include<vector>
#include<queue>
#include<cstdio>
#include<iostream>
using namespace std; struct Cmp{
bool operator()(const int&t1, const int&t2)
{
return t1>t2;
} }; int main()
{
int n, k;
while(~scanf("%d%d", &n, &k))
{
priority_queue<int, vector<int>,Cmp>que;
while(n--)
{
char op[];
scanf("%s", op);
if(op[]=='I')
{
int val;
scanf("%d", &val);
que.push(val);
while(que.size()>k) que.pop();
}
else printf("%d\n", que.top());
}
}
return ;
}
上面这个代码也不太好, 因为它有了太多的进队和出队, 每一次的进出都是需要维护优先队列的, 所以可以在入队满K个后, 在后来的插入时, 可以先比较一下, 然后决定是否插入。
#include<algorithm>
#include<vector>
#include<queue>
#include<cstdio>
#include<iostream>
using namespace std; struct Cmp{
bool operator()(const int&t1, const int&t2)
{
return t1>t2;
} }; int main()
{
int n, k;
while(~scanf("%d%d", &n, &k))
{
priority_queue<int, vector<int>,Cmp>que;
int t = k;
char op[];
int val;
while(t--)
{
scanf("%s", &op);
scanf("%d", &val);
que.push(val);
}
n = n-k;
while(n--)
{
scanf("%s", &op);
if(op[]=='I')
{
scanf("%d", &val);
if(val>que.top())
que.push(val), que.pop();
}
else printf("%d\n", que.top()); }
}
return ;
}
一般优先队列的定义方法:
priority_queue< Type,vector<Type>,greater<Type> >(小顶堆)
priority_queue< Type,vector<Type>,less<Type> >(大顶堆)
如果是自定义的结构体类型
priority_queue<Node,vector<Node>,cmp>
需要自己自定义结构体类型:
struct cmp
{
bool operator()(const Node &t1,const Node &t2)
{
return t1.b<t2.b;//相当于less,大顶堆
}
};
3.又是一道可以用优先队列解的题。 优先队列的功能还真强大。
http://acm.hdu.edu.cn/showproblem.php?pid=4393
#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std; struct Node{
int F;
int index;
friend bool operator<(Node a, Node b)
{
if(a.F!=b.F) return a.F<b.F;
else return a.index>b.index;
}
}; priority_queue<Node>q[];
int main()
{
int T;
int kase, n, S;
Node a;
scanf("%d", &T);
for(kase=; kase<=T; kase++)
{
scanf("%d", &n);
for(int i=; i<=n; i++)
{
scanf("%d%d", &a.F, &S);
a.index = i;
q[S].push(a);
}
printf("Case #%d:\n", kase);
for(int i=; i<n; i++)
{
int fast = -, id = ;
for(int j=; j<=; j++)
if(!q[j].empty())
{
Node tmp=q[j].top();
if(tmp.F+i*j>fast) fast=tmp.F+i*j, id=j;
else if(tmp.F+i*j==fast&&tmp.index<q[id].top().index) id = j;
}
printf("%d", q[id].top().index);
q[id].pop();
if(i<n-)printf(" ");
else printf("\n");
}
}
return ;
}
STL--queue的更多相关文章
- STL Queue 容器
STL Queue 容器 Queue简介 queue是队列容器,是一种“先进先出”的容器. queue是简单地装饰deque容器而成为另外的一种容器. # ...
- 浅谈C++ STL queue 容器
浅谈C++ STL queue 容器 本篇随笔简单介绍一下\(C++STL\)中\(queue\)容器的使用方法和常见的使用技巧.\(queue\)容器是\(C++STL\)的一种比较基本的容器.我们 ...
- C++ STL - queue常见函数使用解析
C++ STL - queue常见函数使用解析 c++队列模板类的定义在头文件中,queue 模板类需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque ...
- STL<queue>的使用
队列是一种基本的线性数据结构.它满足先进先出(First In ,First Out)的原则. 我们可以应用这种数据结构实现很多复杂的问题.但每次要手写队列的相关函数并不省事,我们便可以应用STL中的 ...
- STL queue 常见用法详解
<算法笔记>学习笔记 queue 常见用法详解 queue翻译为队列,在STL中主要则是实现了一个先进先出的容器. 1. queue 的定义 //要使用queue,应先添加头文件#incl ...
- C++标准模板库(STL)——queue常见用法详解
queue的定义 queue<typename> name; queue容器内元素的访问 由于队列本身就是一种先进先出的限制性数据结构,因此在STL中只能通过front()来访问队首元素, ...
- 2.6 C++STL queue详解
文章目录 2.6.1 引入 2.6.2 代码示例 2.6.3 代码运行结果 总结 2.6.1 引入 首先,在STL中 queue 和 stack 其实并不叫容器(container),而是叫适配器(a ...
- STL --> queue单向队列
queue单向队列 queue 模板类的定义在<queue>头文件中.与stack 模板类很相似,queue 模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器 ...
- STL - queue(队列)
Queue简介 queue是队列容器,是一种"先进先出"的容器. queue是简单地装饰deque容器而成为另外的一种容器. #include <queue> queu ...
- STL queue用法
先进先出 #include<iostream> #include<algorithm> #include<cstdio> #include<stack> ...
随机推荐
- GDCPC2016 省赛随笔
这是第一次参加省赛,第二次参加正式的组队赛. 昨晚很早就睡,早上挺早就睡不着醒了,内心既紧张又激动(虽然赛前和队友说尽力就好,但是还是很怕打铁啊).然后吃过早餐跟随学校大部队来到了中大.发现有好多学校 ...
- ActiveMQ 安装异常
解决方式: 1.确认计算机主机名名称没有下划线: 2.如果是win7,停止ICS(运行-->services.msc找到Internet Connection Sharing (ICS)服务,改 ...
- 使用Symfony 2在三小时内开发一个寻人平台
简介 Symfony2是一个基于PHP语言的Web开发框架,有着开发速度快.性能高等特点.但Symfony2的学习曲线也比 较陡峭,没有经验的初学者往往需要一些练习才能掌握其特性. 本文通过一个快速开 ...
- String 类的常用字符串方法
public class Page106 { /** * 字符串练习第五章 * @param args */ public static void main(String[] args) { Stri ...
- Andorid 编程 系统环境安装
内网环境下安装: 1.配置源 :找到公司内部整理的源文件中的内容,将其内容拷贝到系统 源文件 中,并注释掉所有外网链接(如果公司支持内部环境配置,通常会有一个内部源文件) 2.安装jdk, ecli ...
- 20150624_Andriod _web_service_匹配
using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using S ...
- Codeforces Round #336 Zuma
D. Zuma time limit per test: 2 seconds memory limit per test: 512 megabytes input: standard input ...
- 如何生成excel文件作为图像识别结果
如何生成excel文件作为图像识别结果 在进行大规模图像处理的时候,如果能够以表格的形式生成结果文件,将非常的直观.这个时候,选择excel作为结果输出文件,将是合适的. 查询相关资料,有很多关于ex ...
- 2013 Asia Regional Changchun I 题,HDU(4821),Hash
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4821 解题报告:搞了很久,总算搞出来了,还是参考了一下网上的解法,的确很巧,和上次湘潭的比 ...
- ThreadLocal深入理解二
转载:http://doc00.com/doc/101101jf6 今天在看之前转载的博客:ThreadLocal的内部实现原理.突然有个疑问, 按照threadLocal的原理, 当把一个对象存入到 ...