NEFU 大一寒假训练八(队列)2020.02.12

Summary

其实 Contest 里写的是 大一寒假训练七(队列)
我这里写成第八个吧,因为栈那个没建 Contest,我把那个算第七个了。
在家呆了一段时间就被抓走上直播课做题了 o_o …
纪念一下仅有 2 次的一遍过的时刻 ヾ(≧▽≦*)o
在这里插入图片描述

Information

No. Title AC/Submit
A 报数-队列-约瑟夫环 73/100
B 取牌游戏-队列-SET 63/95
C 酒桌游戏-队列 51/67
D 海港-队列 28/85
E 关系网络-队列 10/15
F Blash数集-队列-set 18/27
G 周末舞会-队列 70/78

Problem A: 报数-队列-约瑟夫环 (1634) [73/100]

Tips

简单的队列题,报到 m 的出队
最后输出剩下的一个

Code

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

int main()
{
	queue<int>q;
	int n,m,cnt=0;
	cin>>n>>m;
	for(int i=1;i<=n;i++)q.push(i);
	while(q.size()!=1)
	{
		cnt++;
		if(cnt==m)
		{
			cnt=0;
			q.pop();
			continue;
		}
		q.push(q.front());
		q.pop();
	}
	cout<<q.front();
	return 0;
}

Problem B: 取牌游戏-队列-SET (1633) [63/95]

Tips

题目的意思就是 输出小明拿到牌的编号
先把所有牌存入队列,按照要求发牌,当 cnt%n==0 时为小明拿到的牌
把小明拿到的牌存入数组排序输出即可。

Code

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

int main()
{
	queue<int>q;
	int n,k,p,cnt=0,good[100001],gn=0;
	cin>>n>>k>>p;
	for(int i=1;i<=k;i++)
	{
		q.push(i);
	}
	while(!q.empty())
	{
		cnt++;
		if(cnt%n==0)good[gn++]=q.front();
		q.pop();
		for(int i=0;i<p;i++)
		{
			q.push(q.front());
			q.pop();
		}
	}
	sort(good,good+gn);
	for(int i=0;i<gn;i++)
	{
		cout<<good[i]<<endl;
	}
	return 0;
}

Problem C: 酒桌游戏-队列 (1635) [51/67]

Tips

可以使用结构体存储人名和对应的编号。
由于名字的长度题中没有给出,所以可以使用 string 类型存储姓名。
再把包含7的都踢出去,剩下的一个即为答案。

Code

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

struct People
{
	string name;
	int num;
}p;

int contain7(int num)
{
	int bit;
	if(num%7==0)return 1;
	while(num>0)
	{
		bit=num%10;
		num/=10;
		if(bit==7)return 1;
	}
	return 0;
}

int main()
{
	queue<People>q;
	int n,m,t;
	cin>>n>>m>>t;
	
	for(int i=1;i<=n;i++)
	{
		cin>>p.name;
		p.num=i;
		q.push(p);
	}
	while(q.front().num!=m)
	{
		q.push(q.front());
		q.pop();
	}
	while(q.size()!=1)
	{
		if(!contain7(t))q.push(q.front());
		q.pop();
		t++;
	}
	cout<<q.front().name;
	return 0;
}

Problem D: 海港-队列 (1636) [28/85]

Tips

one of 课上讲的例子,嘿嘿
结构体以人为单位进行存储,分别存储时间和国家,然后再存储在队列中。
再将国家进行桶排,如果原来桶是空的,答案 +1,
查询时先去掉 24小时以前船上的人,如果这个国家的桶空了,答案 -1。

Code

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

struct People
{
	int t;
	int c;
}p;

int main()
{
	queue<People>q;
	int n,ans=0,t,k,c,bucket[300001]={0};
	cin>>n;
	while(n--)
	{
		cin>>t>>k;
		while(k--)
		{
			cin>>c;
			q.push({t,c});
			if(bucket[c]==0)ans++;
			bucket[c]++;
		}
		while(t-q.front().t>=86400)
		{
			bucket[q.front().c]--;
			if(bucket[q.front().c]==0)ans--;
			q.pop();
		}
		cout<<ans<<endl;
	}
	return 0;
}

Problem E: 关系网络-队列 (1663) [10/15]

Tips

可以利用队列进行 BFS 搜索,可能还有更好的方法。
按照人的关系搜索,从 x 搜到 y。
注意搜索时去重,避免重复搜索同一个人。

Code

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

int main()
{
	queue<int>q;
	int n,x,y,mapp[100][100],vis[100]={0},cnt=0,num;
	cin>>n>>x>>y;
	q.push(x-1);
	vis[x-1]=1;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>mapp[i][j];
		}
	}
	while(!q.empty())
	{
		//cout<<"Searching "<<q.front()<<endl;
		num=q.size();
		cnt++;
		for(int i=0;i<num;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(mapp[q.front()][j]&&!vis[j])
				{
					//cout<<"Next is "<<j<<endl;
					if(j==y-1)
					{
						cout<<cnt-1;
						return 0;
					}
					vis[j]=1;
					q.push(j);
					mapp[q.front()][j]++;
				}
			}
			q.pop();
		}
	}
	return 0;
}F

Problem F: Blash数集-队列-set (1662) [18/27]

Tips

这个题难度有点大,偷窥了一下 PPT

<方案一> set:
使用 set 进行排序加去重。
Set 无法直接输出第 n 个数,需要使用迭代器循环查找,然后再输出。

<方案二> unique:
生成数据的存入数组,先排序再用 unique 去重。

注意:无论使用哪种方案,要计算前 n 个数,都需要计算 10*n 个数。

Code

这里采用 set 解法

扫描二维码关注公众号,回复: 9238803 查看本文章
#include <bits/stdc++.h>
using namespace std;

int main()
{
	queue<long long>q;
	set<long long>s;
	long long a,n,cnt=0;
	cin>>a>>n;
	q.push(a);
	while(s.size()!=n*10)
	{
		s.insert(q.front());
		q.push(q.front()*2+1);
		q.push(q.front()*3+1);
		q.pop();
	}
	for(set<long long>::iterator it=s.begin();it!=s.end();it++)
	{
		cnt++;
		if(cnt==n)
		{
			cout<<*(it)<<endl;
			return 0;
		}
	}
	return 0;
}

Problem G: 周末舞会-队列 (1632) [70/78]

Tips

一道签到 (shui) 题,完全可以不用队列嘛 ヾ(≧▽≦*)o

Code

#include <stdio.h>

int main()
{
	int m,n,k,mn=1,nn=1;
	scanf("%d %d %d",&m,&n,&k);
	for(int i=0;i<k;i++)
	{
		printf("%d %d\n",mn,nn);
		mn=mn+1>m?1:mn+1;
		nn=nn+1>n?1:nn+1;
	}
	return 0;
}
发布了62 篇原创文章 · 获赞 202 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/csg999/article/details/104276251