一、队列
1.queue的声明
queue声明的基本结构是这样的:queue<数据结构>队列名;
queue<int> i;
queue<double> d;
queue<node> n; // node是一个结构体
2.queue的基本操作
q.size();//返回q里元素个数
q.empty();//返回q是否为空,空则返回1,否则返回0
q.push(k);//在q的末尾插入k
q.pop();//删掉q的第一个元素
q.front();//返回q的第一个元素
q.back();//返回q的末尾元素
二、优先队列
1.优先队列的声明
priority_queue<结构类型> 队列名;
priority_queue <int> i;
priority_queue <double> d;
不过,我们最为常用的是这几种:priority_queue <node> q; //node是一个结构体,结构体里重载了‘<’小于符号
priority_queue <int,vector<int>,greater<int> > q; (从小到大)//注意后面两个“>”不要写在一起
priority_queue <int,vector<int>,less<int> >q;(从大到小)
2.优先队列的基本操作
q.size();//返回q里元素个数
q.empty();//返回q是否为空,空则返回1,否则返回0
q.push(k);//在q的末尾插入k
q.pop();//删掉q的第一个元素
q.top();//返回q的第一个元素
3.优先队列的特性->自动排序
3.1默认的优先队列(非结构体结构)
priority_queue <int> q;
#include<cstdio>
#include<queue>
using namespace std;
priority_queue <int> q;
int main()
{
q.push(10),q.push(8),q.push(12),q.push(14),q.push(6);
while(!q.empty())
printf("%d ",q.top()),q.pop();
}
结果是14 12 10 8 6,从大到小排序!
3.2默认的优先队列(结构体,重载小于)
#include<cstdio>
#include<queue>
using namespace std;
struct node // 这个node结构体有两个成员,x和y,它的小于规则是x小者小。
{
int x,y;
bool operator < (const node & a) const
{
return x<a.x;
}
}k;
priority_queue <node> q;
int main()
{
k.x=10,k.y=100; q.push(k);
k.x=12,k.y=60; q.push(k);
k.x=14,k.y=40; q.push(k);
k.x=6,k.y=80; q.push(k);
k.x=8,k.y=20; q.push(k);
while(!q.empty())
{
node m=q.top(); q.pop();
printf("(%d,%d) ",m.x,m.y);
}
}
【输入】(10,100),(12,60),(14,40),(6,20),(8,20)
【输出】(14,40) (12,60) (10,100) (8,20) (6,80)
是按照重载后的小于规则,从大到小排序的
3.3less和greater优先队列
priority_queue <int,vector<int>,less<int> > p;
priority_queue <int,vector<int>,greater<int> > q;
#include<cstdio>
#include<queue>
using namespace std;
priority_queue <int,vector<int>,less<int> > p;
priority_queue <int,vector<int>,greater<int> > q;
int a[5]={10,12,14,6,8};
int main()
{
for(int i=0;i<5;i++)
p.push(a[i]),q.push(a[i]);
printf("less<int>:");
while(!p.empty())
printf("%d ",p.top()),p.pop();
printf("\ngreater<int>:");
while(!q.empty())
printf("%d ",q.top()),q.pop();
}
【输出】less<int>:14 12 10 8 6 greater<int>:6 8 10 12 14
所以,less是从大到小,greater是从小到大。
3.4一个结构体的优先队列,要按照各种不一样的规则排序
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n;
struct node
{
int fir,sec;
void Read() {scanf("%d %d",&fir,&sec);}
}input;
struct cmp1
{
bool operator () (const node &x,const node &y) const
{
return x.fir<y.fir;
}
};//当一个node x的fir值小于另一个node y的fir值时,称x<y
struct cmp2
{
bool operator () (const node &x,const node &y) const
{
return x.sec<y.sec;
}
};//当一个node x的sec值小于另一个node y的sec值时,称x<y
struct cmp3
{
bool operator () (const node &x,const node &y) const
{
return x.fir+x.sec<y.fir+y.sec;
}
};//当一个node x的fri值和sec值的和小于另一个node y的fir值和sec值的和时,称x<y
priority_queue<node,vector<node>,cmp1> q1;
priority_queue<node,vector<node>,cmp2> q2;
priority_queue<node,vector<node>,cmp3> q3;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) input.Read(),q1.push(input),q2.push(input),q3.push(input);
printf("\ncmp1:\n");
while(!q1.empty()) printf("(%d,%d) ",q1.top().fir,q1.top().sec),q1.pop();
printf("\n\ncmp2:\n");
while(!q2.empty()) printf("(%d,%d) ",q2.top().fir,q2.top().sec),q2.pop();
printf("\n\ncmp3:\n");
while(!q3.empty()) printf("(%d,%d) ",q3.top().fir,q3.top().sec),q3.pop();
}
【输入】
7
1 2
2 1
6 9
9 6
-100 100
-500 20
4000 -3000
【输出】cmp1:
(4000,-3000) (9,6) (6,9) (2,1) (1,2) (-100,100) (-500,20)cmp2:
(-100,100) (-500,20) (6,9) (9,6) (1,2) (2,1) (4000,-3000)cmp3:
(4000,-3000) (6,9) (9,6) (1,2) (2,1) (-100,100) (-500,20)
三、例题
1.https://www.luogu.com.cn/problem/P1090
#include <bits/stdc++.h>
using namespace std;
int n,ans=0,x;
priority_queue<int,vector<int>,greater<int> > q;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>x;
q.push(x);
}
while(q.size()>=2)
{
int k=q.top();
q.pop();
int l=q.top();
q.pop();
ans+=k+l;
q.push(k+l);
}
cout<<ans<<endl;
return 0;
}
2.https://www.luogu.com.cn/problem/P2058
思路:
先读进人数,再读进每个人的国籍,把人到来的时间和国籍放进队列。去重计数(因为对答案没贡献)。
其次,队列并不要记录每个结构体,它只要记住主时间线之前24小时内的结构体即可。
所以我们先全记下来,再判重,之后再进行队列和答案的更新,最后输出答案即可(答案其实不用清零,因为之前的答案有可能会对现在的做贡献,这样就不用每次读入都遍历了)
#include<cstdio>
#include<queue>
#define rei register int
using namespace std;
int n, t, m, x;
int temp_nation[1000005];
int ans;
struct node
{
int s, t;
//made in china
};
queue<node>ship;
node h;
int main()
{
scanf("%d",&n);
for(rei i=1;i<=n;i++)
{
scanf("%d%d",&t,&m);
while(!ship.empty())//只要还有人就对队列进行检查
{
h = ship.front();//循环取队头(由于时间是单调递增的)
if(h.t+86400<=t)//如果在时间外(不符合条件),则对答案和队列进行更新(删减)
{
temp_nation[h.s]--;//这个国籍人数总数减1(因为这不是24小时内的人)
if(temp_nation[h.s]==0) ans--;//如果这个国籍没有人了,则答案数减1(之前记过)
ship.pop();//把这个被时代所淘汰的人给丢掉
continue;//因为是单调递增的,所以有可能还会有,继续去找
}
break;//如果现在这个在24小时内,后面的肯定也符合条件,直接退出
}
for(rei j=1;j<=m;++j) //查完前面后,我们就对这只本身的船进行统计
{
scanf("%d",&x);//输入国籍
h.s = x, h.t = t;//存进结构体
ship.push(h);//把这个人(结构体)给存进队列
temp_nation[x]++;//这个国籍的人加1(桶排思想)
if(temp_nation[x]==1) ans++;//如果这个国籍是!相对!第一次出现,那么就算上他
}
printf("%d\n",ans);//模拟完了以后就输出答案
}
return 0;
}