C++学习之队列&&优先队列

一、队列

1.定义

类似于栈,但是是从头开始去,放进去的是排在队尾
copy一段

2.基本代码

(1)初始化队列 queue<int>vis,定义一个队列 //vis 是名字,可自由替换 || int是变量类型,如果是结构体就是结构体名称
(2)入队 vis.push(x)
(3)出队 vis.pop()
(4)判断队列是否为空 vis.empty()
(5)判断队列中元素的数量vis.size()
(6)得到队列的队首元素 vis.front()
ps:如果是c语言,就要加头文件#include < queue >

3.题

1.报数-队列-约瑟夫环

在这里插入图片描述
没有什么坑

#include <bits/stdc++.h>
using namespace std;
queue<int>q;
int main()
{
    int n,m,i,k=0,s;
    cin>>n>>m;
    for(i=1;i<=n;i++)
        q.push(i);
    while(q.size()>1)
    {
            s=q.front();
            q.pop();
            k++;
            if(k%m!=0)//让第m个离开,其他人继续放进去
               q.push(s);
    }
    printf("%d",q.front());
    return 0;
}
2.取牌游戏-队列-SET(不是set做法)

在这里插入图片描述在这里插入图片描述
本题注意的问题:
当剩下的牌数小于p时,也是要翻的!
例如: p=3; 还剩下的编号 为4 和5;
第1次翻:5 4;
第2次翻:4 5;
第3次翻:5 4;
也是要翻3次的,哪怕就剩下1张牌了,也要翻P次!

#include <bits/stdc++.h>
using namespace std;
int a[100002];//用数组记下good牌的位置
queue<int>q;
int main()
{
    int n,k,p,i,m,tmp;
    int num=0,w=0;
    cin>>n>>k>>p;
    m=k/n;//有几张good牌
    for(i=1; i<=k; i++)
        q.push(i);
    while(!q.empty())//根据题目意思,每一次给大家发牌,都应该有一张good牌
    {
        tmp=q.front();
        q.pop();
        num++;
        if(num%n==0)//看是不是good牌是就放进数组
            a[w++]=tmp;//因为小明是发牌员,所以能看牌
        if(!q.empty())
            for(i=1; i<=p; i++)//按要求放牌
            {
                int y=q.front();
                q.pop();
                q.push(y);
            }
    }
    sort(a,a+m);//排序输出就行
    for(i=0; i<m; i++)
        printf("%d\n",a[i]);
    return 0;
}

3.酒桌游戏-队列

在这里插入图片描述
在这里插入图片描述
这题是第一题的升级版
来了来了,酒桌逢7游戏
这题是结构体&&字符串&&队列&&结构体数组的混合运用

#include <bits/stdc++.h>
using namespace std;
struct s
{
    int num;
    string name;
} p[1005];
queue<s>q;
bool judge(int x)
{
    if(x%7==0) return 1;//不仅能被7整除
    while(x)
    {
        if(x%10==7) return 1;//个位数是7也要出局
        x=x/10;//检查其它位数是不是7
    }return 0;
}
int main()
{
    int n,m,t,i;
    cin>>n>>m>>t;
    for(i=1; i<=n; i++)
    {
        cin>>p[i].name;//先放入结构体中
        p[i].num=i;//记下脚标
    }
    for(i=1; i<=n; i++)
        q.push(p[i]);//放入队列中
    for(i=1; i<=m-1; i++)//排序,让第m个人去到第一个位置
    {
        q.push(p[i]);
        q.pop();
    }
    while(q.size()>1)
    {
        s tmp=q.front();
        q.pop();
        if(!judge(t))
            q.push(tmp);
        t++;
    }
    printf("%s\n",q.front().name.c_str());//这个输出的格式,简直是精华!!!
    return 0;
}

4.海港-队列

在这里插入图片描述
在这里插入图片描述
用结构体处理,桶排序思想

#include <bits/stdc++.h>
using namespace std;
struct s{
    int t;
    int x;
};
queue<s>q;
int n,t,k,i,x,num[300005],ans=0;
int main()
{
    s tmp;
    cin>>n;
    for(i=1; i<=n; i++)
    {
        cin>>t>>k;
        for(int j=1; j<=k; j++)
        {
            cin>>x;
            q.push({t,x});
            if(num[x]==0)
                ans++;
            num[x]++;
        }
        while(t-q.front().t>=86400)
        {
            tmp=q.front();
            q.pop();
            int x1=tmp.x;
            num[x1]--;
            if(num[x1]==0)
                ans--;
        }
        cout<<ans<<endl;
    }
    return 0;
}

5.周末舞会-队列

在这里插入图片描述
标准队列题

#include <bits/stdc++.h>
using namespace std;
queue<int>vis1,vis2;
int main()
{
    int m,n,k,a1,b1;
    int i;
    cin>>m>>n;
    cin>>k;
    for(i=1; i<=m; i++)
        vis1.push(i);
    for(i=1; i<=n; i++)
        vis2.push(i);
    for(i=1; i<=k; i++)
    {
        a1=vis1.front();
        vis1.pop();
        b1=vis2.front();
        vis2.pop();
        printf("%d %d\n",a1,b1);
        vis1.push(a1);
        vis2.push(b1);
    }
    return 0;
}

二、优先队列

1.定义

在这里插入图片描述

在这里插入图片描述在这里插入图片描述

2.基本代码

方法名说明
q.empty()检测是否为空
q.size()返回实际数据个数
q.top()返回顶端元素
q.push(x)插入一个元素x
q.pop()移除队首元素

在这里插入图片描述

3.题

1.买饭-优先队列

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
struct node
{
    int kk,yy;
};
bool operator < (const node &s1,const node &s2)
{
    if(s1.kk!=s2.kk)return s1.kk>s2.kk;
    return  s1.yy>s2.yy;
}
priority_queue<node>q;//不需要vector
int n,i,x;
int main()
{
    cin>>n;
    for(i=1; i<=n; i++)
    {
        cin>>x;
        q.push({x,i});
    }
    double sum=0.0,t=0.0;
    while(!q.empty())
    {
        node p=q.top();
        q.pop();
        sum=sum+t;
        t=p.kk*1.0+t;
        q.empty()?printf("%d\n",p.yy):printf("%d ",p.yy);//?的用法,前者是真,后者是假
    }
    printf("%.2lf\n",sum/(n*1.0));
    return 0;
}

注意比较函数的书写方法。

2.合并果子-优先队列

在这里插入图片描述
标准优先队列题

#include <bits/stdc++.h>
using namespace std;
priority_queue<long,vector<long>,greater<long> >q;
long int n,i,x1,x2,x,sum=0;
int main()
{
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>x;
        q.push(x);
    }
    while(q.size()>1)
    {
        x1=q.top();q.pop();
        x2=q.top();q.pop();
        q.push(x1+x2);
        sum=x1+x2+sum;
    }
    printf("%ld\n",sum);
    return 0;
}
3.合成陨石-优先队列

在这里插入图片描述
这一题和上一题没什么区别

#include <bits/stdc++.h>
using namespace std;
priority_queue<long,vector<long>,greater<long> >q;
long int n,i,x1,x2,x,k=1;
int main()
{
    while(scanf("%d",&n)!=-1)
    {
        int sum=0;
        for(i=1; i<=n; i++)
        {
            cin>>x;
            q.push(x);
        }
        while(q.size()>1)
        {
            x1=q.top();q.pop();
            x2=q.top();q.pop();
            q.push(x1+x2);
            sum=x1+x2+sum;
        }
        printf("%ld\n",sum);
        q.pop();
    }
    return 0;
}
4.堆-优先队列

在这里插入图片描述
正常做

#include <bits/stdc++.h>
using namespace std;
priority_queue<int,vector<int>,greater<int> >q;
int x,n,k,i;
int main()
{
    scanf("%d",&n);
    for(i=1; i<=n; i++)
    {
        scanf("%d",&k);
        if(k==1)scanf("%d",&x),q.push(x);
        else if(k==2)printf("%d\n",q.top());
        else q.pop();
    }
    return 0;
}
5.瑞瑞的木板-优先队列

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
priority_queue<long long,vector<long long>,greater<long long> >q;
long long n,i,x1,x2,x,sum=0;
int main()
{
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>x;q.push(x);
    }
    while(q.size()>1)
    {
        x1=q.top();q.pop();
        x2=q.top();q.pop();
        q.push(x1+x2);
        sum=x1+x2+sum;
    }
    printf("%lld\n",sum);
    return 0;
}
6.桐桐的新闻系统-优先队列

在这里插入图片描述
着重考比较函数的书写

#include <bits/stdc++.h>
using namespace std;
int x,y,k;string tp;
struct p
{
    int id,per,val;
};
int operator < (const p &a,const p &b)
{
    if (a.val!=b.val) return (a.val>b.val);
    else
        return (a.id > b.id);
}
priority_queue<p>vis;
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>tp)
    {
        if (tp=="#") break;
        cin>>x>>y;
        vis.push({x,y,y});
    }
    cin>>k;
    p tmp;
    for(int i=1;i<=k;i++)
    {
        tmp=vis.top();
        cout<<tmp.id<<endl;
        vis.push({tmp.id,tmp.per,tmp.val+tmp.per});
        vis.pop();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
c STL优先队列是一种数据结构,它能够按照一定的优先级来管理元素。引用中提到了一些关于优先队列的使用方法和操作。在C++中,我们可以使用priority_queue模板类来实现优先队列。默认情况下,优先队列是大堆,也就是优先级高的元素是值较大的元素。如果我们想要控制成小堆,也就是优先级高的元素是值较小的元素,我们可以使用priority_queue的第三个参数,即比较函数。引用中的代码示例展示了如何使用priority_queue来创建优先队列,以及如何改变默认的堆顺序。引用中提到,默认情况下,优先队列使用vector作为底层存储数据的容器,并使用堆算法将vector中的元素构造成堆的结构。因此,我们可以在任何需要使用堆的地方考虑使用优先队列。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [C++STL队列以及优先队列小结](https://blog.csdn.net/Hollay/article/details/88677775)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [C++STL之优先级队列详解](https://blog.csdn.net/attemptendeavor/article/details/121914810)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tancy.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值