题意:

每一个数字有自己所属的团队,如果所属的团队已经有人在队列里,放在团队的最后一个,要不然放队列里最后一个

注意:一个团队里的最多1000个元素,但是入队,出队的操作会达到200000次

解法:循环队列,声明一个n长的数组,数组的每一个元素都是一个循环队列

没有判断队列是否满了,只简单的判断是否为空

AC时间:570ms

主要耗时在查找元素是那个团队时循环了所有元素,可以简单粗暴使用STL,用了就没意义了,o(︶︿︶)o 唉!!!

#include<iostream>
#include <stdio.h>
#include <memory.h>
using namespace std; struct Node
{
int num;
int team;
Node()
{
num = -1;
team = -1;
}
};
struct Queue
{
int ql;
Node node[1003];
int position;
int limit;
Queue()
{
ql = 1003;
position = 0;
limit = 0;
}
void push(Node n)
{
node[position]=n;
position=(position+1)%ql;
}
Node offer()
{
Node n= node[limit];
limit=(limit+1)%ql;
return n;
}
bool empty()
{
return limit == position;
}
}; int findTeam(int number, Node* node, int nl)
{
for(int i = 0; i < nl; i++)
{
if(node[i].num == number)
{
return node[i].team;
}
}
return -1;
}
int findQueue(int team, Queue*head, int tl)
{
for(int i = 0; i < tl; i++)
{
if(head[i].node[0].team == team)
{
return i;
}
}
return tl;
}
int main()
{
//freopen("d:\\1.txt", "r", stdin);
int n;
string eq = "ENQUEUE";
string de = "DEQUEUE";
string stop = "STOP";
int t = 0;
while (cin >> n)
{
if(n == 0)
return 0;
t++;
cout << "Scenario #" << t << endl;
Node node[n * 1003];
Queue head[n];
int tn = 0;
for(int i = 0; i < n; i++)
{
Queue h;
head[i] = h;
int m;
cin >> m;
for(int j = 0; j < m; j++)
{
Node node2;
cin >> node2.num;
node2.team = i;
node[tn++] = node2;
}
}
string str;
int number;
int tl = 0;
while (true)
{
cin >> str;
if(str == stop)
break;
else if(str == de)
{
//de
cout << head[0].offer().num << endl;
if(head[0].empty())
{
int i = 1;
for(i = 0; i < tl-1; i++)
{
head[i] = head[i+1];
}
head[tl-1].limit = 0;
head[tl-1].position = 0;
tl--;
}
}
else
{
//en
cin >> number;
int team = findTeam(number, node, tn);
int i = findQueue(team,head, tl);
Node nn;
nn.num = number;
nn.team = team;
if(head[i].position == 0)
tl++;
head[i].push(nn);
}
}
cout << endl;
}
return 0;
}

  

UVA540-队列的更多相关文章

  1. Team Queue (uva540 队列模拟)

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

  2. 团体队列UVA540 Team Queue(队列简单用法)

    题目背景 队列和优先级队列是大多数计算机科学家都知道的数据结构.但是团队队列却不被人熟知,尽管在生活中经常出现.比如,午餐时间的食堂门口的队列就是一个团队队列.在一个团队队列中,每个元素属于一个团队. ...

  3. ACM学习历程——UVA540 Team Queue(队列,map:Hash)

    Description   Team Queue   Team Queue  Queues and Priority Queues are data structures which are know ...

  4. 算法习题---5.6团体队列(Uva540)

    一:题目 有t个队伍的人正在排队,每次新来一个人,如果他有队友在排队,那他可以插队,直接排到他的队伍的末尾.如果没有队伍在前面,那么他直接排在长队的末尾 ENQUEUE x 将编号x的队员入队 DEQ ...

  5. UVa540 Team Queue(队列queue)

    队列 STL队列定义在头文件<queue>中, 用“ queue<int>s ” 方式定义, 用push()和pop()进行元素的入队和出队操作, front()取队首元素(但 ...

  6. 5_6 团体队列(UVa540)<queue与STL其他容器的综合运用>

    先给出T个团体,并给出每个团体有多少人和每个人的编号,然后所有团体一起排队,排成一条大队列,排队的原则是,一个成员加入,如果这个成员所在的团体已经有人在排队了,那么他就加到他所在团体的最后面,而不是整 ...

  7. 团体队列(UVa540)

    题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...

  8. 团体队列 UVA540 Team Queue

    题目描述 有t个团队的人正在排一个长队.每次新来一个人时,如果他有队友在排队,那么新人会插队到最后一个队友的身后.如果没有任何一个队友排队,则他会被排到长队的队尾. 输入每个团队中所有队员的编号,要求 ...

  9. uva 540 - Team Queue(插队队列)

    首发:https://mp.csdn.net/mdeditor/80294426 例题5-6 团体队列(Team Queue,UVa540) 有t个团队的人正在排一个长队.每次新来一个人时,如果他有队 ...

  10. 【UVA - 540】Team Queue (map,队列)

    Team Queue Descriptions: Queues and Priority Queues are data structures which are known to most comp ...

随机推荐

  1. 轻松制作儿童趣味算术软件 - imsoft.cnblogs

    轻松制作儿童趣味算术软件 马震安 电脑爱好者 2014-07-23 08:38技巧 0 条评论 标签:软件   兴趣是学习的动力,以动感的软件和自动判断得分的形式测试孩子的算术能力,总要比在白纸上出几 ...

  2. BZOJ3757: 苹果树【树上莫队】

    Description ​ 神犇家门口种了一棵苹果树.苹果树作为一棵树,当然是呈树状结构,每根树枝连接两个苹果,每个苹果都可以沿着一条由树枝构成的路径连到树根,而且这样的路径只存在一条.由于这棵苹果树 ...

  3. hdu1238 Substrings 扩展KMP

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  4. Executors Future Callable 使用场景实例

    https://www.jb51.net/article/132606.htm: 我们都知道实现多线程有2种方式,一种是继承Thread,一种是实现Runnable,但这2种方式都有一个缺陷,在任务完 ...

  5. StreamSets 管理 SDC Edge上的pipeline

    可选的方式: ui (data colelctor) 发送命令 UI 主要是创建edge pipeline 的时候进行edge server 的配置 默认是 http://localhost:1863 ...

  6. Centos安装git2.2.1

    由于Centos6.5使用yum -y install git 安装的git版本是 git --versiongit version 1.7.1 想要升级到2.2.1: ># yum remov ...

  7. 在 php 7.3 中 switch 语句中使用 continue

    在 php 7.3 中 switch 语句中使用 continue 在 php 7.3 的 switch 中使用 continue 会出现警告.1 2 3 while ($foo) { switch ...

  8. [boost] : asser库用法

    基本用法 需要包含头文件#include <boost/assert.hpp> assert库定义了两个断言宏 BOOST_ASSERT BOOSE_ASSERT_MSG 第一种形式等价于 ...

  9. PHP图像处理

    1.创建画布: $img=imagescreatetruecolor(200,200); 创建颜色并填充 $red=imagecolorallocate($img,255,0,0);  //创建颜色 ...

  10. 织梦调用文章 ID (来源:百度知道)

    问:{dede:field.id /} {dede:channel type='son' orderby='sortrank'} <a href='[field:typeurl/]'>&l ...