2020.2.12 大一寒假训练七(队列)

Problem A:NEFU1634 报数

约瑟夫环问题。

#include<bits/stdc++.h>
using namespace std;
queue<int> child;
int main()
{
    ios::sync_with_stdio(false);
    int n, m, tot=0;
    cin>>n>>m;
    for(int i=1; i<=n; i++)
        child.push(i);
    while(child.size() > 1)
    {
        tot++;
        if(tot%m)
        {
            int tmp = child.front();
            child.pop();
            child.push(tmp);
        }
        else
            child.pop();
    }
    cout<<child.front()<<endl;
    return 0;
}

Problem B:NEFU1633 取牌游戏

#include<bits/stdc++.h>
using namespace std;
queue<int> pock;
int a[100010];
int main()
{
    ios::sync_with_stdio(false);
    int n, k, p;
    cin>>n>>k>>p;
    for(int i=1; i<=k; i++)
        pock.push(i);
    int cnt=0, po=0;
    while(!pock.empty())
    {
        int tmp = pock.front();
        pock.pop();
        cnt++;
        if(!(cnt%n)) a[++po]=tmp;
        if(!pock.empty())
            for(int i=1; i<=p; i++)
            {
                pock.push(pock.front());
                pock.pop();
            }
    }
    sort(a+1, a+k/n+1);
    for(int i=1; i<=k/n; i++)
        cout<<a[i]<<endl;
    return 0;
}

Problem C:NEFU1635 酒桌游戏

曾经有段不堪回首的 往事

#include<bits/stdc++.h>
using namespace std;
/*
struct node{
    string name;
    int ii;
};
queue<node> alco;*/
bool judge(int num)
{
    while(num)
    {
        if(num%10==7) return 1;
        num /= 10;
    }
    return 0;
}
int main()
{
    ios::sync_with_stdio(false);
    int n, m, t;
    string name;
    queue<string> he;
    cin>>n>>m>>t;
    for(int i=1; i<=n; i++)
    {
        cin>>name;
        he.push(name);
    }
    for(int i=1; i<m; i++)
    {
        he.push(he.front());
        he.pop();
    }
    while(he.size() > 1)
    {
        if(t%7==0 || judge(t))
        {
            he.pop();
        }
        else
        {
            he.push(he.front());
            he.pop();
        }
        t++;
    }
    cout<<he.front()<<endl;
    return 0;
}

直接操作字符串即可。

Problem D:NEFU1636 海港

#include<bits/stdc++.h>
using namespace std;
struct node{
    int time, country;
}tmp;
queue<node> vis;
int main()
{
    ios::sync_with_stdio(false);
    int n, t, k, x, ans=0;
    int num[100010];
    cin>>n;
    memset(num, 0, sizeof(num));
    for(int i=1; i<=n; i++)
    {
        cin>>t>>k;
        for(int j=1; j<=k; j++)
        {
            cin>>x;
            vis.push({t, x});
            if(!num[x]) ans++;
            num[x]++;
        }
        while(t-vis.front().time >= 86400)
        {
            tmp = vis.front();
            vis.pop();
            num[tmp.country]--;
            if(!num[tmp.country]) ans--;
        }
        cout<<ans<<endl;
    }
    return 0;
}

Problem E:NEFU1663 关系网络

BFS。以后会提。

#include<bits/stdc++.h>
#define maxn 120
using namespace std;
struct node{
    int No; // 编号
    int cnt; // 步数
};
queue<node> q;
int n, x, y, a[maxn][maxn];
bool flag[maxn];
void bfs()
{
    q.push({x, 0});
    flag[x] = true;
    while(!q.empty())
    {
        node tmp = q.front();
        q.pop();
        if(tmp.No == y)
        {
            cout<<tmp.cnt-1<<endl;
            return;
        }
        for(int i=1; i<=n; i++)
            if(a[tmp.No][i] && !flag[i])
            {
                flag[i] = true;
                q.push({i, tmp.cnt+1});
            }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>x>>y;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            cin>>a[i][j];
    bfs();
    return 0;
}

Problem F:NEFU1662 Blash数集

使用了三个队列。
其实一个足矣:https://blog.csdn.net/whispers_/article/details/76149448

#include<bits/stdc++.h>
using namespace std;
/*
int q1[100010];
queue<int> q2, q3;*/
queue<int> q1, q2, q3;
int main()
{
    ios::sync_with_stdio(false);
    int a, n;
    cin>>a>>n;
    q1.push(a);
    while(n-1)
    {
        q2.push(2*q1.front()+1);
        q3.push(3*q1.front()+1);
        int t = min(q2.front(), q3.front());
        if(q2.front()<q3.front()) q2.pop();
        
        else if(q2.front()>q3.front()) q3.pop();
        else
        {
            q2.pop();
            q3.pop();
        }
        //cout<<q2.front()<<endl;
        //cout<<q3.front()<<endl;
        if(t != q1.front())
        {
            
            q1.pop();
            q1.push(t);
        }
        n--;
        //cout<<q1.front()<<endl;
    }
    cout<<q1.front()<<endl;
    return 0;
}

Problem G:NEFU1632 周末舞会

#include<bits/stdc++.h>
using namespace std;
queue<int> man, woman;
int main()
{
    ios::sync_with_stdio(false);
    int n, m, k;
    cin>>n>>m>>k;
    for(int i=1; i<=n; i++)
        man.push(i);
    for(int i=1; i<=m; i++)
        woman.push(i);
    for(int i=1; i<=k; i++)
    {
        int a1 = man.front();
        man.pop();
        int a2 = woman.front();
        woman.pop();
        cout<<a1<<" "<<a2<<endl;
        man.push(a1);
        woman.push(a2);
    }
    return 0;
}
发布了58 篇原创文章 · 获赞 40 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/baidu_41248654/article/details/104278212
今日推荐