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 ...
随机推荐
- 第二部分之AOF持久化(第十一章)
AOF持久化是通过保存Redis服务器所执行的写命令来记录数据库状态的.被写入AOF文件的所有命令都是以Redis的命令请求协议格式(纯文本)保存的. 一,AOF持久化的实现 1.命令追加 当AOF持 ...
- 创建SVN源库钩子
在源库的hooks目录下面添加post-commit.bat文件,每次代码该文件会自动执行以保证同步到备份服务器 set SVN_HOME="D:\Program Files\VisualS ...
- k8s简单的来部署一下tomcat,并测试自愈功能
前言: 2018年12月6日 今天终于把k8s运行tomcat打通了,耗了我几天时间一个一个坑踩过来,不容易啊,废话不多说. 先记录一些操作时的错误: <<<<<< ...
- 在一台计算机上运行两个或多个tomcat
有的时候我们需要在一台计算计算机上安装多个Tomcat,那我们该怎样配置呢? ## 1.配置思路: 在操作之前我们先启动一个Tomcat,看一下启动数据: 这里面有我们在环境变量中配置的CATALI ...
- SSH 协议的 ssh StrictHostKeyChecking
项目的SFTP用到了这个参数: @Override public PooledObject<ChannelSftp> makeObject() throws Exception { JSc ...
- vim编辑器的命令总结
1. 设置显示行数 :set nu 2. 复制第100至第103行的代码
- 洛谷 P1443 马的遍历
终于遇到一个简单纯粹一点的bfs了...... 题目链接:https://www.luogu.org/problemnew/show/P1443 题目是求到达一个点的最短步数 也就是说我只要bfs遍历 ...
- 七、Java多人博客系统-2.0版本-docker部署
docker是当下很热门的技术,是对之前的部署系统方式的彻底改变.之前部署系统,需要安装数据库.初始化数据库,安装jdk,配置jdk,部署应用程序,修改配置文件等,很繁琐.一般现场运维人员很难搞定,现 ...
- 基于JavaCv并发读取本地视频流并提取每帧32位dhash特征
1.读取本地视频流,pom依赖 依赖于 org.bytedeco下的javacv/opencv/ffmpeg 包 <dependency> <groupId>org.byted ...
- docker的安装,升级,与删除(最新版)
docker安装在ubuntu上 以前叫做 Docker engine安装现在叫做docker-ce的 第一种安装办法: root下执行,sudo su - root apt-get update - ...