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> ...
随机推荐
- JavaScript,base64加密解密
直接下载吧: http://files.cnblogs.com/files/xiluhua/base64Decode.js
- Centos7下用命令下载jdk7及jboss-eap-6
计划在南非的一台云主机上搭建一个web环境,首先需要在云主机上搭建我们指定版本的JDK和JBOSS. 在云上搭特定版本的环境,软件包传输是一件十分艰巨的任务.我先后尝试了:公司电信专线.公司联通专线. ...
- Yahoo! s4和Twitter storm的粗略比较
- 搭建LAMP
RPM包和源码包存放位置 /usr/local/src 源码包编译安装位置 /usr/local/apache /usr/local/mysql /usr/local/php 默认MySQL 数据库位 ...
- openlayers 学习笔记一
1. 创建地图,加载控件 var map = new OpenLayers.Map("map", { projection: new OpenLayers.Projection(& ...
- [转]iOS应用程序生命周期(前后台切换,应用的各种状态)详解
转载地址:http://blog.csdn.net/totogo2010/article/details/8048652 iOS的应用程序的生命周期,还有程序是运行在前台还是后台,应用程序各个状态的变 ...
- Cube Stacking
Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 21350 Accepted: 7470 Case T ...
- MSSQL删除字段时出现 服务器: 消息 5074,级别 16,状态 1,行 1 的解决办法
有的朋友在做用户维护字段的界面时,肯定发现一个问题,当用脚本:ALTER TABLE 表名 DROP COLUMN 字段名进行删除字段的操作时,会出现“服务器: 消息 5074,级别 16,状态 1, ...
- EasyUI DataGrid 添加排序
这个事例演示了如何在点击列头的时候排序DataGrid中全部的列可以通过点击列头被排序.你可以定义可以被排序的列.默认的,列不能被排序除非你设置sortable属性为TRUE,下面是例子:标记 < ...
- IOSView翻转扭矩位移
CoreGraphics.h CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI_2);[xxx setTransform: ...