UVA 540 Team Queue

#include<stdio.h>
#include<string.h>
int Q[5005][1005];
int hash[1000000];

int main() {
	int T = 0, n, m;
	int last = 1003, num = 1004;
	while (scanf("%d", &n), n) {
		printf("Scenario #%d\n", ++T);
		memset(Q, -1, sizeof(Q));
		//输入队伍
		for (int i = 0; i < n; i++) {
			scanf("%d", &m);
			int t;
			for(int j = 0; j < m; j++) {
				scanf("%d", &t);
				hash[t] = i;
			}
		}
		char s[10];
		int flag = 1;
		while (scanf("%s", s) && s[0] != 'S') {
			//入栈
			if (s[0] == 'E') {
				int x, i, j;
				scanf("%d", &x);
				int tag = 0;
				//找到x要插入的team
				for (i = 1; ; i++) {
					if (hash[x] == Q[i][0] && Q[i][num] != 0) {
						tag = 1;
						break;
					}
					if (Q[i][0] == -1)
						break;
				}
				//如果x能够插队
				if (tag) {
					++Q[i][last];
					Q[i][Q[i][last]] = x;
					++Q[i][num];
				}
				//x不能插队
				else {
					Q[i][1] = x;
					Q[i][0] = hash[x];
					Q[i][last] = Q[i][num] = 1;
				}
			}
			//出栈
			else 
				for (int j = 1; ; j++) {
					if (Q[flag][j] != -1) {
						printf("%d\n", Q[flag][j]);
						Q[flag][j] = -1;
						--Q[flag][num];
						if (Q[flag][num] == 0) 
							++flag;
						break;
					}
				}
		}
		printf("\n");
	}
	return 0;
}

你可能感兴趣的:(UVA 540 Team Queue)