首页 技术 正文
技术 2022年11月6日
0 收藏 823 点赞 1,080 浏览 3611 个字

题目链接: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 DescriptionQueues
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.

 InputThe
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.

 OutputFor
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 Input2
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 OutputScenario #1
101
102
103
201
202
203

Scenario #2
259001
259002
259003
259004
259005
260001

 Source University of Ulm Local Contest 1998 题意: 模拟题,模拟排队

 //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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,027
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,365
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,780
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,857