Team Queue(STL练习题)
题目链接: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
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 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.
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.
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
101
102
103
201
202
203
Scenario #2
259001
259002
259003
259004
259005
260001
//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练习题)的更多相关文章
- hdu 1387(Team Queue) STL
Team Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- ACM学习历程——UVA540 Team Queue(队列,map:Hash)
Description Team Queue Team Queue Queues and Priority Queues are data structures which are know ...
- POJ 2259 Team Queue(队列)
题目原网址:http://poj.org/problem?id=2259 题目中文翻译: Description 队列和优先级队列是大多数计算机科学家已知的数据结构. 然而,Team Queue并不是 ...
- UVA540 Team Queue——题解 by hyl天梦
UVA540 Team Queue 题解 题目描述:题目原题 https://vjudge.net/problem/UVA-540 Queues and Priority Queues are dat ...
- 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: 2000MS Memory Limit: 65536K Total Submissions: 2471 Accepted: 926 Descrip ...
- Team Queue(多队列技巧处理)
Team Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
随机推荐
- 转:一篇讲线上优化查 CPU的脚本
原文链接:https://my.oschina.net/leejun2005/blog/1524687 摘要: 本文主要针对 Java 服务而言 0.背景 经常做后端服务开发的同学,或多或少都遇到 ...
- UVA 10891 Game of Sum
题目大意就是有一个整数串,有两个人轮流取,每次可以取走一个前缀或后缀.两人都足够聪明,且都会使自己收益最大.求取完后先手比后手多多少. 每次我看见上面那句就会深感自己的愚笨无知. 所以来推推性质? 1 ...
- node.js stream
stream是一个接口,流是可以从一个读取或写入数据的目标对象 ,Node 中有很多对象实现了这个接口 一.nodejs stream类型 1. Readable - 可读操作. Writable ...
- 3.Nginx常用功能介绍
Nginx常用功能介绍 Nginx反向代理应用实例 反向代理(Reverse Proxy)方式是指通过代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器,并且从内部网络服 ...
- Golang 网络爬虫框架gocolly/colly 三
Golang 网络爬虫框架gocolly/colly 三 熟悉了<Golang 网络爬虫框架gocolly/colly一>和<Golang 网络爬虫框架gocolly/colly二& ...
- Linux(CentOS6.5)修改默认yum源为国内的阿里云、网易yum源
官方的yum源在国内访问效果不佳. 需要改为国内比较好的阿里云或者网易的yum源 修改方式: echo 备份当前的yum源 mv /etc/yum.repos.d /etc/yum.repos.d.b ...
- JMeter参数化实现
参数化:指对每次发起的请求,参数名称相同,参数值进行替换,如登录三次系统,每次用不同的用户名和密码. 1.1.1. 从csv文件读取(CSV Data Set Config) 步骤: 1)新建一个文 ...
- 小白的Python之路 day4 软件目录结构规范
软件目录结构规范 为什么要设计好目录结构? "设计项目目录结构",就和"代码编码风格"一样,属于个人风格问题.对于这种风格上的规范,一直都存在两种态度: 一类同 ...
- e.target和this的区别
```e.target与this的区别 event.target表示发生点击事件的元素this表示注册点击事件的元素 this 等于 e.currentTarget 指的是现在的目标this是所有原生 ...
- Java---hashCode()和equals()
1.hashCode()和equals() API hashCode()和equals()都来自上帝类Object, 所有的类都会拥有这两个方法,特定时,复写它们. 它们是用来在同一类中做比较用的,尤 ...