2020大一寒假ACM培训⑦(queue队列篇)

队列相关讲解请看:队列:彻底理解队列
(这篇文章是用Java实现的,不过学的时候理解就可以了)

特点:先进先出

队列的基本操作:
(1)初始化队列 queue<int>vis,定义一个队列

操作代码实现
入队vis.push(x)
出队vis.pop()
判断队列是否为空vis.empty()
判断队列中元素的数量vis.size()
得到队首元素vis.front()
得到队尾元素vis.back()

注:queue容器不允许顺序遍历,没有迭代器成员函数

头文件: #include <queue> ;

nefu 1632 周末舞会-队列

#include <bits/stdc++.h>

using namespace std;
int x,y,n,c,d;
queue <int>a;
queue <int>b;
int main()
{
   cin>>x>>y;
   cin>>n;
   for(int i=1;i<=x;i++){
    a.push(i);
   }
   for(int i=1;i<=y;i++){
    b.push(i);
   }
   for(int i=1;i<=n;i++){
    c=a.front();
    d=b.front();
    printf("%d %d\n",c,d);
   a.pop();
   b.pop();
   a.push(c);
   b.push(d);
   }
   return 0;
}

nefu 1633 取牌游戏-队列

不停的出队入队,根本停不下来。

#include <bits/stdc++.h>

using namespace std;
int n,k,p,a[100005],f,x,t,j=0;
queue <int> s;
int main()
{
    cin>>n>>k>>p;
    for(int i=1; i<=k; i++)
        s.push(i);
    while(!s.empty())
    {
        f=s.front();
        s.pop();
        t++;
        if(t%n==0)
            a[j++]=f;
        if(!s.empty())
        for(int i=1; i<=p; i++)
        {
            x=s.front();
            s.pop();
            s.push(x);
        }
    }
    sort(a,a+j);
    for(int i=0;i<j;i++)
    printf("%d\n",a[i]);
    return 0;
}

nefu 1634 报数-队列

接着水

#include <bits/stdc++.h>

using namespace std;
int n,m,t;
queue <int> a;

int main()
{
   cin>>n>>m;
   for(int i=1;i<=n;i++){
    a.push(i);
   }
   while(a.size()!=1){
    for(int i=1;i<m;i++){
      t=a.front();
      a.push(t);
      a.pop();
    }
    a.pop();
   }
   printf("%d",a.front());
   return 0;
}

nefu 1635 酒桌游戏-队列

用string很香!

#include <bits/stdc++.h>

using namespace std;
int n,m,t;
string c;
queue <string> s;
int hq(int a){
  int f=0;
  int gw;
  while(a){
    gw=a%10;
    if(gw==7){
        f++;
    }
    a=a/10;
  }
  if(f) return 1;
  else return 0;
}
int main()
{
   cin>>n>>m>>t;
   for(int i=1;i<=n;i++){
    cin>>c;
    s.push(c);
   }
   for(int i=1;i<m;i++){
    string tmp(s.front());
    s.pop();
    s.push(tmp);
   }
   while(s.size()!=1){
    if(hq(t)){
        s.pop();
        t++;
    }
    else if(t%7==0){
        s.pop();
        t++;
    }
    else{
    string cp(s.front());
    s.pop();
    s.push(cp);
    t++;
   }
   }
   cout<<s.front()<<endl;
   return 0;
}

nefu 1662 Blash数集-队列-set

学习jwGG的思想,嘿嘿

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll a[100005],x,y,t,n,x1,x2,i;
int main()
{
    scanf("%lld%lld",&t,&n);
    a[1]=t;
    x1=x2=1;
    for(i=2;i<=n;i++){
    x=a[x1]*2+1;
    y=a[x2]*3+1;
    a[i]=min(x,y);
      if(a[i]==x){x1++;}
      if(a[i]==y){x2++;}
    }
    printf("%d\n",a[n]);
    return 0;
}

nefu 1636 海港

这题数组要开大否则会re。

#include <bits/stdc++.h>

using namespace std;
struct sa{
 int t;
 int x;
};
int t,nn,n[100001],m,ans,k;
queue <sa> s;
int main()
{
    struct sa tmp;
    cin>>nn;
    while(nn--){
     cin>>t>>k;
    while(k--){
        cin>>m;
        if(n[m]==0){
            ans++;
        }
        n[m]++;
       s.push({t,m});
    }
    while(t-s.front().t>=86400){
        tmp=s.front();
        s.pop();
        n[tmp.x]--;
        if(n[tmp.x]==0) ans--;
    }
    printf("%d\n",ans);
    }
    return 0;
}

nefu 1663 关系网络-队列

这个题涉及到了广搜的知识,用广搜实现对最小路径的查找。(现 学 现 卖)
不了解广搜的小伙伴可以看这几篇博客
广搜(BFS)
BFS(广搜)和DFS(深搜)算法原理(通俗易懂版)
深搜和广搜(初学者)

#include <bits/stdc++.h>
using namespace std;
int n,xx,y,t[110],a[110][110];
struct sa{
int x,ans;
};
queue <sa> s;
int main()
{
    cin>>n>>xx>>y;//接收起点xx和终点y
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
        cin>>a[i][j];
        }
    int cut=0;
    s.push({xx,cut});//将起点压入队列
    t[xx]++;//对入过队列的元素进行标记
    while(!s.empty())
    {
      int x=s.front().x;//对记录队首元素
        cut=s.front().ans;
     if(x==y) break;//若发现最小路径,则返回
      s.pop();//出队
      for(int i=1;i<=n;i++)
      {
          if(a[x][i]&&t[i]==0)//若与x认识且未入过队,入队
          {
              s.push({i,cut+1});
              t[i]++;
          }
      }
    }
    printf("%d\n",cut-1);//因为题目问的是经过几个人,所以将步数减一
    return 0;
}

如果有时间,会更新多解。(鸽 前 预 警)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值