题目链接: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. java二维码生成代码

    QRCodeUtil.encode(text, "D:/004.jpg", "D:", true, "exp");// 这个方法的第一个参数 ...

  2. 解决WebService/WCF调用时报错"服务器提交了协议冲突. Section=ResponseStatusLine"问题

    今天更新了一个网站,新增了一个页面,调用WebService,在测试环境好好的,部署到正式环境后就莫名报错: 服务器提交了协议冲突. Section=ResponseStatusLine 网上查了好多 ...

  3. c#创建access数据库和数据表

      由于在程序中使用了ADOX,所以先要在解决方案中引用之,方法如下: 解决方案资源管理器(项目名称)-->(右键)添加引用-->COM--> Microsoft ADO Ext. ...

  4. 使用Python批量下载ftp服务器中的内容

    使用ftplib,轻松实现从ftp服务器上下载所需要的文件,包括目录结构等,支持了一下断点续传 from ftplib import FTP import sys import os import r ...

  5. XUL透明异形旋转窗体

    200行不到的代码,实现透明异形旋转窗体. 下载(25MB): http://oltag.com:8080/yaolixing/18/11/00/OHUIv52.0.1_3_webTrans20180 ...

  6. SAP中的BOPF(Business Object Processing Framework)

    希望简化你的业务应用开发过程?业务对象处理框架(Business Object Processing Framework,以下简称BOPF)也许可以帮到你. BOPF是SAP Business Sui ...

  7. Swift语言中与C/C++和Java不同的语法(四)

    这一节,我们将会讨论一下Swift中的函数相关的基本内容 首先是函数的创建: func sayHello (name:String) -> String { return "Hello ...

  8. windows 下运行angualr/material2 项目

    第一步:到github上clone  angular/material2 项目 第二步:npm install 第三步: 打开git bash (cmd 或 powershell 是无法成功运行该项目 ...

  9. 通过 python的 __call__ 函数与元类 实现单例模式

    简单一句话,当一个类实现__call__方法时,这个类的实例就会变成可调用对象. 直接上测试代码 class ClassA: def __call__(self, *args, **kwargs): ...

  10. [js高手之路] es6系列教程 - new.target属性与es5改造es6的类语法

    es5的构造函数前面如果不用new调用,this指向window,对象的属性就得不到值了,所以以前我们都要在构造函数中通过判断this是否使用了new关键字来确保普通的函数调用方式都能让对象复制到属性 ...