1. 程式人生 > >UVA 540 Team Queue 題意很重要!!!!!

UVA 540 Team Queue 題意很重要!!!!!

#include <stdio.h>
#include <string.h>

struct Q
{
	int el[1005];
	int qu[1005];
	int qu_num;
} q[1005];

int v[1000005];
int output[1000005];
int t;
int i , j;
int qnum;
int tqq;
char sb[20];
int main()
{
	int st = 1;
	while (scanf("%d", &t) != EOF && t)
	{
		int tt = 0;
		memset(q, 0 ,sizeof(q));
		memset(v, 0, sizeof(v));
		memset(output, 0, sizeof(output));
		for (i = 0; i < 1005; i ++)
			for (j = 0; j < 1005; j ++)
				q[i].qu[j] = -1;
		for (i = 0; i < t; i ++)
		{
			scanf("%d", &qnum);
			for (j = 0; j < qnum; j ++)
			{
				scanf("%d", &q[i].el[j]);
				v[q[i].el[j]] = i;
			}
		}
		getchar ();
		while (scanf("%s", sb) != EOF && strcmp(sb, "STOP") != 0)
		{
			if (strcmp(sb, "ENQUEUE") == 0)
			{
				int sbb;
				scanf("%d", &sbb);
				int tq = 0;
				while (v[q[tq].qu[0]] != v[sbb] && q[tq].qu[0] != -1)
				{
					tq ++;
				}
				q[tq].qu[q[tq].qu_num ++] = sbb; 
			}
			if (strcmp(sb, "DEQUEUE") == 0)
			{
				tqq = 0;
				while (q[tqq].qu[0] != -1)
				{
					tqq ++;
				}
				i = 0;
				output[tt ++] = q[0].qu[0];
				q[0].qu[0] = -1;
				for (j = 1; j < q[0].qu_num; j ++)
				{
					q[0].qu[j - 1] = q[0].qu[j];
				}
				q[0].qu_num --;
				q[0].qu[q[0].qu_num] = -1;
				if (q[0].qu[0] == -1)
				{
					int k;
					for (k = 1; k < tqq; k ++)
					{
						q[k - 1].qu_num = q[k].qu_num;
						for (j = 0; j < q[k].qu_num; j ++)
						{
							q[k - 1].qu[j] = q[k].qu[j];
						}
					}
					for (j = 0; j < q[tqq - 1].qu_num; j ++)
					{
						q[tqq - 1].qu[j] = -1;
					}
					q[tqq - 1].qu_num = 0;
				}
			}
		}
		printf("Scenario #%d\n", st ++);
		for (i = 0; i < tt; i ++)
		{
			printf("%d\n", output[i]);
		}
		printf("\n");
	}
	return 0;
}