您的位置:首页 > 产品设计 > UI/UE

Poj2259 Team Queue 【队列】

2013-04-07 05:20 417 查看
http://poj.org/problem?id=2259

队列是一种先进先出的数据结构。它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称作队尾,进行删除操作的前段称作队首。队列中没有元素时,称作空队列。

题目大意:在组队队列中,每个元素(element)属于一支队伍。如果一个元素将要进入组队队列时,它会先从头到尾先检查他的队友(同属一支队伍)是否已经在队列中,如果找到了,他就会紧随其后进入队伍。如果没有找到,则他会从队列末尾入队,并成为自己队伍的第一个元素。出队操作则跟普通队列一样:元素在组队队列中按从头到尾的顺序出列。

假设现在有n个队伍,就开n个队列,其中q[i]表示第i个队列;另外再开一个队列qq记录team前后的信息。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
template <class T> void checkmin(T &t,T x) {if(x < t) t = x;}
template <class T> void checkmax(T &t,T x) {if(x > t) t = x;}
template <class T> void _checkmin(T &t,T x) {if(t==-1) t = x; if(x < t) t = x;}
template <class T> void _checkmax(T &t,T x) {if(t==-1) t = x; if(x > t) t = x;}
typedef pair <int,int> PII;
typedef pair <double,double> PDD;
typedef long long ll;
#define foreach(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end ; it ++)
queue <int> q[1010];
queue <int> qq;
char od[22];
int belong[1000100];
int cntq[1010];
int main() {
int n , cas = 1;
while(~scanf("%d",&n) && n) {
printf("Scenario #%d\n" , cas ++);
for(int i=0;i<n;i++) while(!q[i].empty()) q[i].pop();
while(!qq.empty()) qq.pop();
for(int i=0;i<n;i++) {
int m;
scanf("%d",&m);
while(m --) {
int a;
scanf("%d",&a);
belong[a] = i;
}
cntq[i] = 0;
}
while(scanf("%s",od) && od[0] != 'S') {
if(od[0] == 'E') {
int a , b;
scanf("%d",&a);
b = belong[a];
q[b].push(a);
if(cntq[b] == 0) qq.push(b);
cntq[b] ++;
}
else {
int a = qq.front();
cntq[a] --;
if(cntq[a] == 0) qq.pop();
int b = q[a].front();
printf("%d\n",b);
q[a].pop();
}
}
/*
while(!qq.empty()) {
int a = qq.front();
qq.pop();
int b = q[a].front();
q[a].pop();
}
*/
puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: