Team Queue (HDU:1387)
In a team queue each element belongs to a team. If an element enters the queue, it first searches the queue from head to tail to check if some of its teammates (elements of the same team) are already in the queue. If yes, it enters the queue right behind them. If not, it enters the queue at the tail and becomes the new last element (bad luck). Dequeuing is done like in normal queues: elements are processed from head to tail in the order they appear in the team queue.
Your task is to write a program that simulates such a team queue.
InputThe input will contain one or more test cases. Each test case begins with the number of teams t (1<=t<=1000). Then t team descriptions follow, each one consisting of the number of elements belonging to the team and the elements themselves. Elements are integers in the range 0 - 999999. A team may consist of up to 1000 elements.
Finally, a list of commands follows. There are three different kinds of commands:
ENQUEUE x - enter element x into the team queue
DEQUEUE - process the first element and remove it from the queue
STOP - end of test case
The input will be terminated by a value of 0 for t.
OutputFor each test case, first print a line saying "Scenario #k", where k is the number of the test case. Then, for each DEQUEUE command, print the element which is dequeued on a single line. Print a blank line after each test case, even after the last one.
Sample Input
2
3 101 102 103
3 201 202 203
ENQUEUE 101
ENQUEUE 201
ENQUEUE 102
ENQUEUE 202
ENQUEUE 103
ENQUEUE 203
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
2
5 259001 259002 259003 259004 259005
6 260001 260002 260003 260004 260005 260006
ENQUEUE 259001
ENQUEUE 260001
ENQUEUE 259002
ENQUEUE 259003
ENQUEUE 259004
ENQUEUE 259005
DEQUEUE
DEQUEUE
ENQUEUE 260002
ENQUEUE 260003
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
0
Sample Output
Scenario #1
101
102
103
201
202
203 Scenario #2
259001
259002
259003
259004
259005
260001
#include<iostream>
#include<map>
#include<queue>
#include<string>
using namespace std;
int main()
{
int n;
int k = ;
while (cin >> n, n != )
{
k++;
cout << "Scenario #" << k << endl;
int visit[] = { };
map<int, int>team;
queue<int> que;
queue<int> q[];
for (int i = ; i < n; i++)
{
int t,temp;
cin >> t;
while (t--)
{
cin >> temp;
team[temp] = i;
}
}
string sr;
while (cin >> sr, sr != "STOP")
{
if (sr == "ENQUEUE")
{
int tp;
cin >> tp;
q[team[tp]].push(tp);
if (visit[team[tp]] == )
{
visit[team[tp]] = ;
que.push(team[tp]);
}
}
else
{
cout << q[que.front()].front() << endl;
q[que.front()].pop();
if (q[que.front()].empty()) visit[que.front()] = ,que.pop();
}
}
cout << endl;
}
return ;
}
//对于这题,刚开始的思路有问题,总是想着怎么用优先队列解决问题,把思路放在了怎样去定义结构体和怎样定义比较函数,想了半天完全不行,所以在网上看了一下大佬的代码,核心思路就是队列里面放队列,这方法真是简单粗暴,再使用一个数组标记那个队列已放入,厉害,,,,,,
Team Queue (HDU:1387)的更多相关文章
- 【UVA - 540】Team Queue (map,队列)
Team Queue Descriptions: Queues and Priority Queues are data structures which are known to most comp ...
- uva 540 - Team Queue(插队队列)
首发:https://mp.csdn.net/mdeditor/80294426 例题5-6 团体队列(Team Queue,UVa540) 有t个团队的人正在排一个长队.每次新来一个人时,如果他有队 ...
- UVA 540 Team Queue(模拟+队列)
题目代号:UVA 540 题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page ...
- 2道acm编程题(2014):1.编写一个浏览器输入输出(hdu acm1088);2.encoding(hdu1020)
//1088(参考博客:http://blog.csdn.net/libin56842/article/details/8950688)//1.编写一个浏览器输入输出(hdu acm1088)://思 ...
- C/C++解题常用STL大礼包 含vector,map,set,queue(含优先队列) ,stack的常用用法
每次忘记都去查,真难啊 /* C/C++解题常用STL大礼包 含vector,map,set,queue(含优先队列) ,stack的常用用法 */ /* vector常用用法 */ //头文件 #i ...
- Team Queue(多队列技巧处理)
Team Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- ACM学习历程——UVA540 Team Queue(队列,map:Hash)
Description Team Queue Team Queue Queues and Priority Queues are data structures which are know ...
- War Chess (hdu 3345)
http://acm.hdu.edu.cn/showproblem.php?pid=3345 Problem Description War chess is hh's favorite game:I ...
- Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5064 题目意思:给出n个数:a1, a2, ..., an,然后需要从中找出一个最长的序列 b1, b ...
随机推荐
- Mac之日常操作
1.创建root用户使用最高权限 sudo passwd root 一般情况下,使用临时获取最高权限 sudo vim /etc/shells 2. apache操作 #启动Apache sudo a ...
- Windows 虚拟机 忘记密码的处理
说明 经过验证 没法用这种方式处理 之前的系统够可以 2016的方法 稍后在写一个. 1. 修改虚拟机的配置界面: 2. 增加windows的安装盘 作为启动盘 3 bios 里面设置CD启动 比较简 ...
- 在Ubuntu上使用离线方式快速安装K8S v1.11.1
在Ubuntu上使用离线方式快速安装K8S v1.11.1 0.安装包文件下载 https://pan.baidu.com/s/1nmC94Uh-lIl0slLFeA1-qw v1.11.1 文件大小 ...
- Python--Linux上安装Python
Linux 上安装 Python 官网下载:https://www.python.org/downloads/ 本文安装包下载链接:https://pan.baidu.com/s/1uL2JyoY_g ...
- C++(1):error: invalid conversion from ‘void (*)()’ to ‘void (*)(int)
void signaldemo_test(void) { struct itimerval tv, otv; signal(SIGALRM, sigFunc); //how long to run t ...
- busybox(四)完善
目录 busybox(四)完善 proc挂载 手动挂载 proc解析 使用脚本自动挂载 使用mount-a挂载 udev/mdev 挂载 使用jffs2 文件系统格式 安装zlib 安装jffs2 生 ...
- GO语言系列(二)- 基本数据类型和操作符
一.文件名 & 关键字 & 标识符 1.所有go源码以.go结尾 2.标识符以字母或下划线开头,大小写敏感 3._是特殊标识符,用来忽略结果 4.保留关键字 二.Go程序的基本结构 p ...
- Recurrent Neural Networks(RNN) 循环神经网络初探
1. 针对机器学习/深度神经网络“记忆能力”的讨论 0x1:数据规律的本质是能代表此类数据的通用模式 - 数据挖掘的本质是在进行模式提取 数据的本质是存储信息的介质,而模式(pattern)是信息的一 ...
- Kubernetes之ServiceAccount
ServiceAccount 是什么 Service Account为Pod中的进程和外部用户提供身份信息.所有的kubernetes集群中账户分为两类,Kubernetes管理的serviceacc ...
- 在鼠标右键上加入使用notepad++编辑【转】
我们在安装完notepad++文本编辑器之后,在一个文本文件上右键有时候并没有出现“使用notepad++编辑的选项”,我们可以通过简单地修改注册表文件来增加这样的功能: 1. 首先打开注册表,wi ...