Team Queue
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 2471 | Accepted: 926 |
Description
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
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.
Warning: A test case may contain up to 200000 (two hundred thousand) commands, so the implementation of the team queue should be efficient: both enqueing and dequeuing of an element should only take constant time.
Output
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"cstring"
#include"string"
#include"cstdio"
using namespace std;
//map<int,int> m;
int m[];
queue<int> q;//队伍序号
queue<int> qq[];//入队
int flag[];
int ncase=;
int t,n,ele;
void inti();
void input();
void solve();
int main()
{
while(scanf("%d",&t)==&&t)
{
inti();
input();
solve();
}
return ;
}
void input()
{
for(int i=;i<t;i++)
{
//cin>>n;
scanf("%d",&n);
for(int j=;j<n;j++)
{
//cin>>ele;
scanf("%d",&ele);
m[ele]=i;
}
}
}
void inti()
{
for(int i=;i<t;i++)
{
flag[i]=;
while(!qq[i].empty())
qq[i].pop();
}
while(!q.empty())
q.pop();
}
void solve()
{
string com;
int a;
//cout<<"Scenario #"<<++ncase<<endl;
printf("Scenario #%d\n",++ncase);
while(cin>>com,com!="STOP")
{
if(com=="ENQUEUE")
{
//cin>>a;
scanf("%d",&a);
if(!flag[m[a]])
{
flag[m[a]]=;
q.push(m[a]);
}
qq[m[a]].push(a);
}
else if(com=="DEQUEUE")
{
int order=q.front();
cout<<qq[order].front()<<endl;
qq[order].pop();
if(qq[order].empty())
{
q.pop();
flag[order]=;
}
}
}
cout<<endl;
}
Team Queue的更多相关文章
- hdu 1387(Team Queue) STL
Team Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Team Queue (uva540 队列模拟)
Team Queue Queues and Priority Queues are data structures which are known to most computer scientist ...
- ACM题目————Team Queue
Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...
- HDU 1387 Team Queue
Team Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Team Queue(多队列技巧处理)
Team Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Team Queue(STL练习题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1387 Team Queue Time Limit: 2000/1000 MS (Java/Others ...
- Team Queue (HDU:1387)
Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...
- Team Queue POJ - 2259 (队列)
Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...
- POJ 2259 - Team Queue - [队列的邻接表]
题目链接:http://poj.org/problem?id=2259 Queues and Priority Queues are data structures which are known t ...
随机推荐
- Android相关图书推荐
疯狂Android讲义(第3版 附光盘) 作 者 李刚 著 出 版 社 电子工业出版社 出版时间 2015-06-01 版 次 3 页 数 780 印刷时间 2015-0 ...
- UVa12657 - Boxes in a Line(数组模拟链表)
题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...
- 修改Map中确定key对应的value问题
今天在码代码的时候出现一个没有预料的问题: 先看下面的代码: public static void main(String[] args) { String[] files=new String[]{ ...
- DelphiXE Android的所有权限按照分类总结说明
相关资料:http://www.delphitop.com/html/Android/2778.html 网络相关的: android.permission.INTERNET 允许程序打开网络套接 ...
- 咏南中间件支持DELPHI低版本开发的两层程序平稳升级到三层
提供DELPHI中间件及中间件集群,有意请联系. N年前,我们用DELPHI低版本开发的两层程序(比如工厂ERP系统),现在仍然在企业广泛地得到使用,但老系统有些跟不上企业的发展需要了.主要表现在:虽 ...
- [iOS基础控件 - 6.10.5] UIApplication
A.概念 1.UIApplication对象是应用程序的象征,每个应用都有 2.单例 3.[UIApplication sharedApplication] 获取 4.iOS启动创建的第一个对象 5. ...
- [置顶] 新修改ADB,支持Android 4.2 系统 ,全部中文命令,手机屏幕截图等等
发过好几个ADB的工具,有很多朋友用了之后给我反馈了不少的意见和bug,这里非常感谢他们,所以今天花了一天的时间重新整理了一下ADB,并且修改了这些BUG.也有朋友建议我给一个修改列表,今天发这个帖子 ...
- HDU1856More is better(并查集)
最近发现以前的东西都忘得差不多了,在这里刷刷水题 并查集: int find_parent(int x) { return x = p[x] ? x : p[x] = find_parent(p[x] ...
- C#学习笔记(十二):正则表达式
Regex 正则表达式的类,我们可以通过该类来使用正则表达式. 比如下面我们使用Regex来判断输入的字符串是否符合指定的格式: using System; using System.Text.Reg ...
- update多表更新的2种方式
update t1 set TermBeginQty =isnull((select top 1 JiaoPlusQty from WMS_RptMaterialPutDaily r where t1 ...