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> ...
随机推荐
- Understanding the RelationshipType Enumeration [AX 2012]
Understanding the RelationshipType Enumeration [AX 2012] 3 out of 3 rated this helpful - Rate this t ...
- bash正则表达式
星号*: 匹配它前面的字符串或正则表达式任意次(包括0次). 比如:* 可能匹配的字符串有:... ...... 句号.: 匹配除换行符之外的任意一个字符. 比如:"112.",将 ...
- 控制反转(IOC)和依赖注入(DI)的区别
IOC inversion of control 控制反转 DI Dependency Injection 依赖注入 要理解这两个概念,首先要搞清楚以下几个问题: 参与者都有谁? 依赖:谁 ...
- PHP的一些常用汇总
1. 使用strcmp()函数[区分大小写] 和strcasecmp()函数按照字节比较.比较结果显示:前和后相同为0,前>后为大于0,前<后为小于0. 2. 格式化字符串:number_ ...
- html+css复习之第2篇 | javascript
1. java 中定义数组和对象: 数组(Array)字面量 定义一个数组: [40, 100, 1, 5, 25, 10] 对象(Object)字面量 定义一个对象: {firstName:&quo ...
- jdk 与jre的区别
jdk就是java的开发工具集,顾名思义就是你做开发用的,其中包括javac,也就是java compiler等. jre(java runtime environment),就是java程序的运行环 ...
- 20150916_001 vba 基础
一.什么是“宏”.“宏”有什么用 关于“宏”的详细定义,可以参考百度百科的解释(点击查看).我给它一个简单的或许不太严谨的定义: 宏的通俗定义:宏是被某些软件所能识别.理解并执行的特定代码/脚本. 宏 ...
- poj1987 Distance Statistics
普通dfs访问每个点对的复杂度是O(n^2)的,显然会超时. 考虑访问到当前子树的根节点时,统计所有经过根的点(u, v)满足: dist(u) + dist(v) <= maxd,并且 bel ...
- Pie(二分POJ3122)
Pie Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12985 Accepted: 4490 Special Ju ...
- Paths on a Grid(简单组合数学)
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 23008 Accepted: 5683 Desc ...