题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1387

Team Queue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1518    Accepted Submission(s): 511

Problem Description
Queues
and Priority Queues are data structures which are known to most
computer scientists. The Team Queue, however, is not so well known,
though it occurs often in everyday life. At lunch time the queue in
front of the Mensa is a team queue, for example.
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.

 
Input
The
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.

 
Output
For
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

 
Source
题意: 模拟题,模拟排队
 //deque 双向队列
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<cstring>
using namespace std;
#define N 1010
deque<int> qu[N];//保存大的队列中的出现的每个小团队
map<int, int> mp;//保存每个人属于哪一个团队
vector <int > vv;//保存大的队列中的每个小团队出现的顺序
void init()
{
mp.clear();
for(int i = ;i < N ; i++)
{
qu[i].clear();
}
vv.clear();
}
int main()
{
int tm;
int cnt = ;
while(~scanf("%d",&tm)&&tm!=)
{
cnt++;
int rs;
init();
for(int i = ; i < tm ; i++)
{
scanf("%d",&rs);
for(int j= ; j < rs ; j++)
{
int t;
scanf("%d",&t);
mp[t] = i;
}
}
char ml[];
printf("Scenario #%d\n",cnt);
vector <int> ::iterator it ;
while(~scanf("%s",ml))
{
if(ml[]=='S') break;
else if(ml[]=='E')
{
int tt;
scanf("%d",&tt);
int fl = mp[tt];
if(!qu[fl].empty())
{
qu[fl].push_back(tt);
}
else
{
it = find(vv.begin(),vv.end(),fl);
if(it != vv.end()) vv.erase(it);
vv.push_back(fl);
qu[fl].push_back(tt);
}
}
else if(ml[]=='D')
{
int flag = ;
while(qu[vv[flag]].empty())
{
flag+=;
}
int o= qu[vv[flag]].front() ;
printf("%d\n",o);
qu[vv[flag]].pop_front();
}
}
puts("");
}
return ;
}

下面给出一个queue 的写法,更新一个知识点,queue的pop函数是用来去除最前面的元素的所以可以直接用pop

 #include<cstdio>
#include<queue>
#include<map>
using namespace std;
const int maxn = ;
int main()
{
int t , kase = ;
while(~scanf("%d",&t),t)
{
printf("Scenario #%d\n",++kase);
//记录所有人的团队编号
map<int , int> team;
for(int i = ;i < t ; i++)
{
int n , x;
scanf("%d",&n);
while(n--){
scanf("%d",&x); team[x] = i;
}
}
//模拟
queue<int> q, q2[maxn];//q是团队的队列,q2是团队i成员的队列
for(;;)
{
int x ;
char cmd[];
scanf("%s",cmd);
if(cmd[]=='S') break;
else if(cmd[] =='D')
{
int t = q.front();
printf("%d\n",q2[t].front());q2[t].pop();
if(q2[t].empty()) q.pop();//团队t全部出队
}
else if(cmd[] == 'E')
{
scanf("%d",&x);
int t = team[x];
if(q2[t].empty()) q.push(t);
q2[t].push(x);
}
}
printf("\n");
}
return ;
}

Team Queue(STL练习题)的更多相关文章

  1. hdu 1387(Team Queue) STL

    Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. ACM学习历程——UVA540 Team Queue(队列,map:Hash)

    Description   Team Queue   Team Queue  Queues and Priority Queues are data structures which are know ...

  3. POJ 2259 Team Queue(队列)

    题目原网址:http://poj.org/problem?id=2259 题目中文翻译: Description 队列和优先级队列是大多数计算机科学家已知的数据结构. 然而,Team Queue并不是 ...

  4. UVA540 Team Queue——题解 by hyl天梦

    UVA540 Team Queue 题解 题目描述:题目原题 https://vjudge.net/problem/UVA-540 Queues and Priority Queues are dat ...

  5. Team Queue (uva540 队列模拟)

    Team Queue Queues and Priority Queues are data structures which are known to most computer scientist ...

  6. ACM题目————Team Queue

    Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...

  7. HDU 1387 Team Queue

    Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  8. Team Queue

    Team Queue Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2471   Accepted: 926 Descrip ...

  9. Team Queue(多队列技巧处理)

    Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

随机推荐

  1. Bash shell命令记录和CentOS的一些技巧

    ①CentOS的实用技巧: 一.按下ctrl+alt+F2可由图形界面切换至命令行(shell窗口),按下ctrl+alt+F1可由命令行切换至图形界面(前提是安装CentOS时软件选择项选择安装了图 ...

  2. bzoj 2302: [HAOI2011]Problem c

    Description 给n个人安排座位,先给每个人一个1~n的编号,设第i个人的编号为ai(不同人的编号可以相同),接着从第一个人开始,大家依次入座,第i个人来了以后尝试坐到ai,如果ai被占据了, ...

  3. 限制ssh远程登陆

    超过十次,就添加到hosts.deny里面去 #!/bin/bash date=`date +%Y%m%d` file="/var/log/secure" max=10 if [[ ...

  4. MySQL index 增删改

    一.前提信息 1.数据库版本 mysql> select version(),user(); +------------+----------------+ | version() | user ...

  5. template package (godoc 翻译)

    template 包 概述(Overview) template 包实现了数据驱动模板用于生成文本输出. 要生成HTML输出,请参阅html/template包,它具有与此包相同的接口,但会自动保护H ...

  6. Android模拟器

    一.Genymotion 1.下载安装:https://www.genymotion.com/download/  (注:安装前需要先注册) 因为Genymotion运行需要VirtualBox,如果 ...

  7. Asp.net 不安全端口 解决chrome浏览器访问时提示:ERR_UNSAFE_PORT

    https://blog.bbzhh.com/index.php/archives/136.html 想在vps做个测试,看看是否25端口屏蔽是否生效,于是起了一个小web服务在25端口做测试,但是使 ...

  8. C# DataGridView中DataGridViewComboBoxCell列,下拉框事件的处理【完美解决】

    http://blog.csdn.net/a312100321/article/details/25195311 问题:DataGridView绑定数据源之后,有一列需要用下拉框DataGridVie ...

  9. Fiddler中Response 的Raw乱码问题解决

    有时候我们看到Response中的HTML是乱码的, 这是因为HTML被压缩了, 我们可以通过两种方法去解压缩. 1. 点击Response Raw上方的"Response body is ...

  10. rpc之thrift

    rpc之thrift 一.介绍 thrift是一个rpc(remove procedure call)框架,可以实现不同的语言(java.c++.js.python.ruby.c#等)之间的相互调用. ...