Team Queue_poj2259

/*
Team Queue
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5712 Accepted: 1932
Description


Queues 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


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


For 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 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<string>
#include<queue>
#include<map>
using namespace std;
int nCaseNum,nNum;
queue<long> nQue[1001]; //存储每个队列 
queue<int> nS; //存储队列号 
int nM[1000000]; //元素与队列号的映射表 
bool nFlag[1001]; //标识有无同组元素 


void solve()
{
string nCommand;
long nElem;
cout<<"Scenario #"<<++nCaseNum<<endl;
while(cin>>nCommand,nCommand!="STOP"){
if(nCommand=="ENQUEUE"){
cin>>nElem;
if(!nFlag[nM[nElem]]) //若还没有同组元素 
{
nFlag[nM[nElem]]=true;
nS.push(nM[nElem]); //将组号进队列 
}
nQue[nM[nElem]].push(nElem);
}else if(nCommand=="DEQUEUE"){
int nId=nS.front(); //首先处理最先进队列的那组元素 
cout<<nQue[nId].front()<<endl;
nQue[nId].pop();
if(nQue[nId].empty()){
nS.pop();
nFlag[nId]=false;
}
}
}
cout<<endl;
}
void inti()//初始化 
{
for(int i=0;i!=nNum;++i){
nFlag[i]=false;
while(!nQue[i].empty())
nQue[i].pop();
}
while(!nS.empty())
nS.pop();
}
void input()
{
int nElem,elemNum;
for(int i=0;i!=nNum;++i){
cin>>elemNum;
for(int j=0;j!=elemNum;++j){
cin>>nElem;
nM[nElem]=i;
}
}
}
int main()
{
nCaseNum=0;
while(cin>>nNum,nNum){
inti();
input();
solve();
}
return 0;
}
### 关于 `queue QUEUE_TYPE_SET` 的使用场景和配置 #### 使用场景 在多线程编程环境中,队列用于管理任务调度和资源分配。对于特定类型的队列集合(即 `QUEUE_TYPE_SET`),其主要应用场景在于: - **统一管理和监控多个队列**:当应用程序中有多种不同类型的任务需要处理时,可以创建一组具有相同特性的队列,并将其作为一个整体进行管理[^1]。 - **提高性能优化灵活性**:允许开发者根据实际需求动态调整各个成员队列的工作模式(如由串行变为并发或反之亦然)。这有助于更好地适应不同的负载情况,从而提升系统的响应速度与吞吐量。 - **简化跨平台开发工作**:某些操作系统可能提供了对这种高级数据结构的支持,使得编写一次代码即可轻松移植到其他平台上运行而无需大幅修改逻辑设计[^3]。 #### 配置方法 为了设置一个基于 `QUEUE_TYPE_SET` 的环境,在大多数情况下会涉及到以下几个方面: ##### 创建队列集 首先定义一个新的队列集合对象,通常可以通过API调用来完成此操作。例如,在GCD (Grand Central Dispatch) 中并没有直接提供名为 `QUEUE_TYPE_SET` 的接口;但是可以根据相似的概念构建类似的机制——通过数组或其他容器类保存多个已创建好的独立队列实例。 ```c // 假设有一个自定义函数来初始化一系列派发队列 dispatch_queue_t *queues; size_t count = 5; // 设定要创建多少个子队列 queues = malloc(sizeof(dispatch_queue_t) * count); for(size_t i=0;i<count;++i){ queues[i]=dispatch_queue_create([NSString stringWithFormat:@"com.example.queue%d",i], NULL); } ``` ##### 添加/移除单个队列 支持向现有集合内增加新的成员或是从中删除不再使用的个体单元。这一过程应当确保不会影响正在执行中的任何作业流程。 ```objc -(void)addQueue:(dispatch_queue_t)newQueue{ [_mutex lock]; if (_activeQueues == nil || ![_activeQueues containsObject:newQueue]) { [_activeQuees addObject:newQueue]; } [_mutex unlock]; } -(void)removeQueue:(dispatch_queue_t)oldQueue{ [_mutex lock]; if ([_activeQueues containsObject:oldQueue]){ [_activeQueues removeObject:oldQueue]; } [_mutex unlock]; } ``` 此处 `_mutex` 是用于保护共享资源访问的安全锁,防止因竞态条件引发的数据竞争问题。 ##### 设置默认行为 指定整个集合的行为准则,比如是采用同步还是异步方式提交新任务给内部各条线路去处理。此外还可以设定全局优先级以及其他通用属性以便更精细地控制运作细节。 ```swift let defaultAttributes : DispatchQueue.Attributes = .concurrent var globalSetPriority : Int8 = qos_class_encoding(.default) func applyGlobalSettings(to set:[DispatchQueue]){ for q in set{ q.setTarget(queue:.global(qos:.background)) q.apply(DispatchWorkItemFlags(rawValue:uint(defaultAttributes.rawValue))) let attr = UnsafeMutablePointer<UInt>.allocate(capacity:MemoryLayout<Int8>.size) memcpy(attr,&globalSetPriority,MemoryLayout<Int8>.size) dispatch_queue_set_specific(q,"priority",(OpaquePointer)(attr),nil) } } ``` 上述 Swift 版本展示了如何遍历传入的队列列表并对它们应用相同的配置选项,包括目标队列的选择、工作项标志位以及特定键值关联的数据存储等特性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值