poj 2259 Team Queue
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 2977 | Accepted: 1092 |
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
- 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
Source
参考了代码,自己打了一遍:
注意:
1.头文件中的<string> <cstring>不要忘记
2.每次的Init()函数不要忘记
//poj 2259 Team Queue
#include<cstdio>
#include<iostream>
#include<queue>
#include<string>//不要忘记!!
#include<cstring>
using namespace std;
short nM[];//元素映射组
bool flag[];//标记组中是否有元素
queue<int> q;//保存组编号
queue<long> nq[];//存储每个队列的元素
void Init(int n){
int i,j,k;
//memset(nM,0,sizeof(nM));
memset(flag,false,sizeof(flag));
while(!q.empty()){
q.pop();
}
for(i=;i<=n;i++){
while(!nq[i].empty()){
nq[i].pop();
}
}
}
void Iuput(int n){
int i,j,k,num;
for(i=;i<n;i++){
flag[i]=false;
scanf("%d",&num);
for(j=;j<num;j++){
cin>>k;
nM[k]=i;//k在队列i中
}
}
}
void Solve(int &n){
string s;
int i;
cout<<"Scenario #"<<n++<<endl;
while(cin>>s,s!="STOP"){
if(s=="ENQUEUE"){
cin>>i;
if(!flag[nM[i]]){//该队伍中的第一个元素
flag[nM[i]]=true;
q.push(nM[i]);
}
nq[nM[i]].push(i);
}
else{
int fir=q.front();
cout<<nq[fir].front()<<endl;
nq[fir].pop();
if(nq[fir].empty()){
q.pop();
flag[fir]=false;
}
}
}
cout<<endl;
}
int main(){
//freopen("D:\\INPUT.txt","r", stdin);
int n,i=;
while(scanf("%d",&n)&&n){
Init(n);
Iuput(n);
Solve(i);
}
return ;
}
poj 2259 Team Queue的更多相关文章
- POJ 2259 - Team Queue - [队列的邻接表]
题目链接:http://poj.org/problem?id=2259 Queues and Priority Queues are data structures which are known t ...
- POJ 2259 Team Queue(队列)
题目原网址:http://poj.org/problem?id=2259 题目中文翻译: Description 队列和优先级队列是大多数计算机科学家已知的数据结构. 然而,Team Queue并不是 ...
- (队列的应用5.3.2)POJ 2259 Team Queue(队列数组的使用)
/* * POJ_2259.cpp * * Created on: 2013年10月30日 * Author: Administrator */ #include <iostream> # ...
- queue POJ 2259 Team Queue
题目传送门 题意:先给出一些小组成员,然后开始排队.若前面的人中有相同小组的人的话,直接插队排在同小组的最后一个,否则只能排在最后面.现在有排队和出队的操作. 分析:这题关键是将队列按照组数分组,用另 ...
- Team Queue POJ - 2259 (队列)
Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...
- 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 ...
随机推荐
- ubuntu 编译安装 mod_wsgi
在编译过程中遇到一些问题,记录下来方便别人使用. step1: 下载.windows下面会有编译好的包,Ubuntu没有需要自己编译. 地址: https://github.com/GrahamDum ...
- Django ajax方法提交表单,及后端接受数据
前台代码: {% block content %} <div class="wrapper wrapper-content"> <div class=" ...
- 关于SVN浏览服务器的错误
这种错误是因为URL错误,需要把https://iZ1gyqtig7Z/svn/BoLeBang/ 换成自己的公网ip地址 https://xx.xx.xx.xxsvn/BoLeBang/ 就可以 ...
- 三个数组求中位数,以及中位数的中位数----java算法实现
求三个数组的中位数,以及中位数的中位数. import java.util.Arrays; public class median { public static void main(String ...
- occal [问题解决]ORA-01427: 单行子查询返回多个行
有人问题我一个问题,情况如下:他要用根据divide_act_channel_day的new_amount字段去更新divide_stat的new_amount字段.两张表关联的条件:day=log_ ...
- 重新理解javascript回调函数
把函数作为参数传入到另一个函数中.这个函数就是所谓的回调函数 经常遇到这样一种情况,某个项目的A层和B层是由不同的人员协同完成.A层负责功能funA,B层负责funcB.当B层要用到某个模块的数据,于 ...
- mysql 命令备份还原
备份 mysqldump -h localhost -uroot -p123456 springbootdb > e:/springbootdb.sql 还原 mysql -h localhos ...
- [BZOJ1935][SHOI2007]Tree 园丁的烦恼(树状数组)
题目描述 很久很久以前,在遥远的大陆上有一个美丽的国家.统治着这个美丽国家的国王是一个园艺爱好者,在他的皇家花园里种植着各种奇花异草. 有一天国王漫步在花园里,若有所思,他问一个园丁道: “最近我在思 ...
- selenium爬取qq空间,requests爬取雪球网数据
一.爬取qq空间好友动态数据 # 爬取qq空间好友状态信息(说说,好友名称),并屏蔽广告 from selenium import webdriver from time import sleep f ...
- modalTransitionStyle各种present效果
coverVertical(默认的) flipHorizontal crossDissolve partialCurl