题目链接: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. Core Erlang:Erlang的Core中间表示

    随着erlang的不断发展,它的语法越来越复杂,不便于诸如分析器,调试器此类程序在源码层次直接进行解析,而CORE Erlang旨在为Erlang提供一个人类可读可改的中间表示(Intermediat ...

  2. NOI2009 植物大战僵尸

    啊一道好题感觉写得挺爽的啊这题这种有一点懵逼然后学了一点东西之后很明朗的感觉真是好!预处理参考 :http://www.cppblog.com/MatoNo1/archive/2014/11/01/1 ...

  3. jQuery中$(function(){})与(function($){})(jQuery)、$(document).ready(function(){})等的区别详细讲解 ----转载

    1.(function($) {-})(jQuery); 1).原理: 这实际上是匿名函数,如下: function(arg){-} 这就定义了一个匿名函数,参数为arg 而调用函数时,是在函数后面写 ...

  4. Fiddler扩展之脚本录制

    Jmeter的脚本来源有4个,此处重点说明第4个 1)手动编写 2)badboy录制 3)自带录制功能 4)Fiddler生成 本文的主要用途:将fiddler抓取的请求,导出为jmx格式,方便jme ...

  5. centos 命令

    1.查看占用端口的进程 netstat -lnp|grep 3000(3000为端口号) Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statis ...

  6. 响应式布局—设备像素密度测试 (-webkit-min-device-pixel-ratio)

      最近遇到这种头疼的问题,百思不得其解,不耻下问,悬梁刺股这些事情都做过之后,终于看到希望,于是攒见好就收,感觉整理分享给大家,希望有所帮助. 对手机分辨率和网页像素的初步认识是,是2倍的差别. 但 ...

  7. PHP按行读取文件 去掉换行符"\n"

    第一种: $content=str_replace("\n","",$content); echo $content; 或者: $content=str_rep ...

  8. umask的作用[转]

    umask的作用 umask 命令允许你设定文件创建时的缺省模式,对应每一类用户(文件属主.同组用户.其他用户)存在一个相应的umask值中的数字.对于文件来说,这一数字的最 大值分别是6.系统不允许 ...

  9. 用eNSP模拟

    eNSP论坛实验示例汇总 http://support.huawei.com/ecommunity/bbs/10168783.html 容易出的问题: 1.输入前是<Huawei>输入sy ...

  10. 用mint ui去实现滚动选择日期并可以关闭拾取器

    转发要备注出处哈,么么哒 注释的那些部分都是我在尝试的时候写得,留给自己看得,删除不影响效果哈,希望对你们有帮助,比较忙可能写得很粗糙,不好意思,有空再改了 实例一:   <template&g ...