当前位置:网站首页>AcWing 132. Group queue (queue simulation question)
AcWing 132. Group queue (queue simulation question)
2022-06-12 10:40:00 【Jacob* ̄▽ ̄*】





The question :
Yes n A group Queue up , There are several people in each group .
When a man comes to the team ,
- If The team already has members of its own team , He just jumped in line behind his team members .
- otherwise , Standing at the back of the line .
Given No more than 2 * 10 ^ 5 Order to join the team ( The number is X People who Come to the team ) and Out of line instructions ( Team leader Out of the team ), Output The order of leaving the team .
Ideas :
This problem obviously needs a lot of Small queues , So for the convenience of , Let's go straight ahead N individual queue As Total queue , namely queue<int>[N]
Use one Hashtable ha, Record All entered member numbers Of Group number .
Use one Array st, Record Enter the group to which the member belongs Whether it appears in The total queue in , If by 0 There has never been , Not for 0 Record at the same time The group Appear in the What paragraph Small queues In .
Then the simulation , See code for details .
Be careful :
- Out of the team When , When The size of a small queue is
0Be sure to remember tostArray Corresponding place clear0.
Code :
#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
map<int, int> ha;// The group number corresponding to the member number
int st[N];// Determine whether members of a group appear in the queue , At the same time, record the group in the sub queue of the group
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';
// When there is no... In the total queue x Members of the group to which they belong , Then open a new small queue and press x
if (!st[ha[x]])
{
st[ha[x]] = ++t;
qq[t].push(x);
}
else // Otherwise, they will be pushed into the small queue of their group
{
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;
}
边栏推荐
- Common regular expressions
- PHP occupies memory
- Get all listening events in the element
- AcWing 135. Maximum subsequence sum (prefix sum + monotone queue to find the minimum value of fixed length interval)
- Summary method of lamp environment deployment
- How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?
- 基于C#的安全聊天工具设计
- ArrayList automatic capacity expansion principle
- How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?
- Common methods of string class
猜你喜欢

890. 查找和替换模式

卡鱼刺别再喝醋吞米饭了!教你2招,让鱼刺安全“跑出来”

AcWing 135. Maximum subsequence sum (prefix sum + monotone queue to find the minimum value of fixed length interval)

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

M-arch (fanwai 13) gd32l233 evaluation - some music

使用cpolar远程办公(2)

2022京东618预售定金怎么退?京东618定金能退吗?

Solution to invalid small program scroll into view

基于QT的旅行查询与模拟系统
![[machine learning] practice of logistic regression classification based on Iris data set](/img/c6/0233545d917691b8336f30707e4636.png)
[machine learning] practice of logistic regression classification based on Iris data set
随机推荐
Pycharm view the current version of opencv
使用cpolar远程办公(2)
Mysql5.6.24 installation free deployment method
Leetcdoe 2037. Make each student have the minimum number of seat movements (yes, once)
A snare - Cookie spoofing
JS obtains the time period of this week and last week (one time period is from Monday to Sunday)
Summary method of lamp environment deployment
Pseudo static setting of access database in win2008 R2 iis7.5
The solution of Lenovo notebook ThinkPad t440 WiFi dropping all the time
Composer command
M-arch (fanwai 13) gd32l233 evaluation - some music
【CF1392D】D. Omkar and Bed Wars(环形与后效性dp)
2022京东618预售定金怎么退?京东618定金能退吗?
Collation of common functions in JS
PHP string encryption and decryption
How to upload the video on the computer to the mobile phone without network
Malicious code analysis practice - lab06-01 exe~Lab06-04. Exe dynamic analysis
Global and local existence of array, integer and character variables
Common tools download address
Download Notepad++