当前位置:网站首页>AcWing 132. 小组队列(队列模拟题)
AcWing 132. 小组队列(队列模拟题)
2022-06-12 10:32:00 【Jacob* ̄▽ ̄*】





题意:
有 n 个小组 要进行排队,每个小组中有若干个人。
当一个人来到队伍时,
- 如果 队伍中已经有自己小组的成员,他就直接插队排在自己小组成员的后面。
- 否则,就站在队伍的最后面。
给定 不超过 2 * 10 ^ 5 个入队指令(编号为 X 的人 来到队伍)和 出队指令(队头的人 出队),输出 出队的顺序。
思路:
这题显然需要用到很多 小队列,因此为了方便,我们先直接开 N 个 queue 作为 总队列,即 queue<int>[N]
用一个 哈希表 ha,记录 所有输入的成员编号 所属的 小组编号。
用一个 数组 st,记录 输入成员所属的小组 是否出现在 总的队列 中,如果 为 0 则未出现过,不为 0 则同时记录 该小组 出现在 第几段 小队列 之中。
之后就是模拟,具体详见代码。
注意:
- 出队 的时候,当 某个小队列的大小为
0一定要记得将st数组 对应处 清0。
代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
map<int, int> ha;//成员编号对应的组别编号
int st[N];//判断有无某个组别的成员出现在队列中,同时记录该组别在第几组小队列中
int main()
{
int T = 1;
int Cnt = 0;
while (~scanf("%d", &T), T)
{
queue<int> qq[N];
memset(st, false, sizeof st);
ha.clear();
int h = 1, t = 0;
while (T--)
{
int cnt; scanf("%d", &cnt);
++t;
for (int i = 0; i < cnt; ++i)
{
int tag; scanf("%d", &tag);
ha[tag] = t;
}
}
char op[20];
h = 1, t = 0;
printf("Scenario #%d\n", ++Cnt);
while (~scanf("%s", op), *op != 'S')
{
if (*op == 'E')
{
int x; scanf("%d", &x);
//cout << "ha[x] " << ha[x] << '\n';
//当总队列中还没有 x 所属组别的成员,则开一个新的小队列并压入 x
if (!st[ha[x]])
{
st[ha[x]] = ++t;
qq[t].push(x);
}
else //否则压入他所属组别的小队列
{
qq[st[ha[x]]].push(x);
}
}
else
{
if (qq[h].size())
{
int top = qq[h].front();
printf("%d\n", top);
qq[h].pop();
if (!qq[h].size())
{
st[ha[top]] = 0;
h++;
}
}
}
}
puts("");
}
return 0;
}
边栏推荐
- This and final keywords
- Add jar package under idea2018 web project
- Win10 professional edition user name modification
- QT custom window fillets
- MySQL user and permission management, role management
- 2022淘宝618超级喵运会玩法来了 超级喵运会有哪些攻略方法
- PHP: Excel to get the letter header
- PHP maximum balance method to solve the problem that the sum of the final percentages is not equal to 100
- 基于C#的安全聊天工具设计
- How the ArrayList collection implements ascending and descending order
猜你喜欢

The solution of Lenovo notebook ThinkPad t440 WiFi unable to connect to the Internet

MySQL performance test (slow query log)

pycharm 查看opencv当前的版本

Set SVG color

Solution to invalid small program scroll into view

Malicious code analysis practice - lab06-01 exe~Lab06-04. Exe dynamic analysis

On 3dsc theory and application of 3D shape context feature

The solution of Lenovo notebook ThinkPad t440 WiFi dropping all the time

数组,整型,字符变量在全局和局部的存在形式

How to play the 2022 Taobao 618 Super Cat Games? Playing skills of 2022 Taobao 618 Cat Games
随机推荐
Failed to load resource: the server responded with a status of 413 (Request Entity Too Large)
[MySQL] learn more about the clustered indexes and auxiliary indexes (b+ tree indexes) in InnoDB
3. Abstract Factory
pycharm 查看opencv当前的版本
PHP uses leanclound to save associated properties
PHP specifies the number of people to distribute the specified amount equally at random (scaling method)
Binassii module - converting between binary and ASCII
Tp6+memcached configuration
Common configuration commands for Cisco network device security management
Pagoda chevereto1.6.2 the latest version of stepping on the pit tutorial in Chinese
基于QT的旅行查询与模拟系统
浅谈三维形状上下文特征3DSC理论及应用
高通平台如何修改特殊电压
MYSQL用户与权限管理,角色管理
PHP Apple internal purchase callback processing
Chromebook system without anti-virus software
Cookie object
93. obtain all IP addresses of the Intranet
golang中的定时器
The name of a great man