Team Queue

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

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
 
 
 
解析:运用map将人按编号映射到所属队伍编号,用一个队列数组q1存储各个队列里人的编号,用一个队列q2存储各个队伍的编号。
 
 
 
#include <bits/stdc++.h>
using namespace std; map<int, int> mp;
char op[10];
int t, n, p, cn = 0; int main()
{
while(scanf("%d", &t), t){
mp.clear();
printf("Scenario #%d\n", ++cn);
for(int i = 0; i < t; ++i){
scanf("%d", &n);
while(n--){
scanf("%d", &p);
mp[p] = i;
}
}
queue<int> q1[1005];
queue<int> q2;
while(scanf("%s", op), op[0] != 'S'){
if(op[0] == 'E'){
scanf("%d", &p);
int id = mp[p];
if(q1[id].empty())
q2.push(id);
q1[id].push(p);
}
else if(op[0] == 'D'){
int id = q2.front();
printf("%d\n", q1[id].front());
q1[id].pop();
if(q1[id].empty())
q2.pop();
}
}
printf("\n");
}
return 0;
}

  

HDU 1387 Team Queue的更多相关文章

  1. HDU 1387 Team Queue( 单向链表 )

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

  2. hdu 1387 Team Queue (链表)

    题目大意: 不同的人在不同的队伍里,插入链表的时候假设这个链表里有他的队友,就把它放到最后一个队友的最后.假设没有队友,就把它放到整个链表的最后面. 出链表的时候把第一个人拿出来. 思路分析: 要模拟 ...

  3. Team Queue(STL练习题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1387 Team Queue Time Limit: 2000/1000 MS (Java/Others ...

  4. hdu 1387(Team Queue) STL

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

  5. Team Queue (HDU:1387)

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

  6. Team Queue (uva540 队列模拟)

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

  7. ACM题目————Team Queue

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

  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. 【C++基础】sizeof 与 strlen的区别

    要理解两者的区别,就要分别理解他们的本质 strlen(char *) 计算字符串的长度,内部实现是用一个循环计算字符串的长度,直到‘\0’为止 1.srtlen 是一个函数,参数只能为char 或者 ...

  2. DJANGO中,用QJUERY的AJAX的json返回中文乱码的解决办法

    和网上其它用JAVA或是PHP的实现不太一样, DJANGO中的解决办法如下: 后端样例: def render_to_json_response(context, **response_kwargs ...

  3. 简单的线程同步问题:两个线程交替执行N次【Synchronized、Lock、ArrayBlockingQueue】

    方法一:传统的线程方法import org.apache.log4j.Logger; /** * 两个线程执行的代码片段要实现同步互斥的效果,它们必须用同一个Lock对象.<br/> * ...

  4. 【Linux高频命令专题(9)】ls

    ls命令是linux下最常用的命令.ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linu ...

  5. 单例模式与Android

    http://blog.csdn.net/ljianhui/article/details/29275655 多线程下的单例模式是不安全的 Android中的单例模式 Android中存在着大量的单例 ...

  6. C语言文件操作函数

    C语言文件操作函数大全 clearerr(清除文件流的错误旗标) 相关函数 feof表头文件 #include<stdio.h> 定义函数 void clearerr(FILE * str ...

  7. IntelliJ IDEA集成svn

    IntelliJ IDEA如何集成svn呢? 1. 首先配置下载并配置svn软件,推荐使用SlikSvn. 下载地址https://sliksvn.com/download/,下载最近版本

  8. IDEA15 File工具栏中没有 Import Project

    使用IDEA准备导入项目时发现没有Import Project选项... 解决办法: Settings > Appearance & Bechavior > Menus and T ...

  9. Android学习之-TextView的滑动效果

    textView中如何设置滚动条 在xml中定义: <TextView            android:layout_width="wrap_content"      ...

  10. 字符设备 register_chrdev_region()、alloc_chrdev_region() 和 register_chrdev()

    1. 字符设备结构体 内核中所有已分配的字符设备编号都记录在一个名为 chrdevs 散列表里.该散列表中的每一个元素是一个 char_device_struct 结构,它的定义如下: static ...