HDU1387:Team Queue

浅谈队列:https://www.cnblogs.com/AKMer/p/10314965.html

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1387

(t+1)个队列,对于每个队伍用一个队列,然后用一个总队列存队伍之间的相对位置即可。

时间复杂度:(O(m))

空间复杂度:(O(n^2))

代码如下:

#include <cstdio>
using namespace std;

const int maxn=2e3+5;

int n;
char opt[20];
int bel[1000000];

int read() {
	int x=0,f=1;char ch=getchar();
	for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
	for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
	return x*f;
}

struct Team_Queue {
	int list[maxn];
	int head,tail;

	bool empty() {
		return head==tail;
	}

	void push_back(int x) {
		list[tail++]=x;
	}

	int pop() {
		int res=list[head];
		head++;return res;
	}
}q[maxn];

void clear() {
	for(int i=0;i<=n;i++)
		q[i].head=q[i].tail=0;
}

int main() {
	int testcase=0;
	while(1) {
		n=read();if(!n)break;clear();
		for(int i=1;i<=n;i++) {
			int cnt=read();
			for(int j=1,x;j<=cnt;j++)
				x=read(),bel[x]=i;
		}
		printf("Scenario #%d
",++testcase);
		while(1) {
			scanf("%s",opt+1);
			if(opt[1]=='E') {
				int x=read();
				if(!q[bel[x]].empty())q[bel[x]].push_back(x);
				else q[0].push_back(bel[x]),q[bel[x]].push_back(x);
			}
			if(opt[1]=='D') {
				int id=q[0].list[q[0].head];
				printf("%d
",q[id].pop());
				if(q[id].empty())q[0].pop();
			}
			if(opt[1]=='S')break;
		}
		puts("");
	}
	return 0;
}
原文地址:https://www.cnblogs.com/AKMer/p/10315291.html