540UVa小团体队列

来源:互联网 发布:诸暨行知小学校园网 编辑:程序博客网 时间:2024/06/08 11:35

两次WA:

1、tmque[eletm[out] ] = -1;   去除一个team时,要把这个team的队列位置标记为-1

2、while(!que.empty())
que.dequeue(emp);

que必须在main外定义时,记得每次用前清空!


#include<stdio.h>#include<string.h>#define MAX 1010template<typename elem>class queue{public:int front;int rear;elem mem[MAX];queue(){front = 1;rear = 0;}int len(){return (rear-front+1+MAX)%MAX;}bool empty(){return (rear+1)%MAX == front;}bool enqueue(const elem& it){if((rear+2)%MAX == front) return false;rear = (rear+1)%MAX;mem[rear] = it;return true;}bool dequeue(elem& it){if(empty()) return false;it = mem[front];front = (front+1)%MAX;return true;}};int eletm[1000010];int tmque[1010];queue<queue<int> > que;int main(){freopen("input.txt","r",stdin);int scena = 1;int numtm;while(scanf("%d", &numtm)==1){if(!numtm) break;printf("Scenario #%d\n", scena++);memset(eletm, 0, sizeof(eletm) );int t;int numele;for(int i = 1; i <= numtm; i++){scanf("%d", &numele);for(int j = 0; j < numele; j++){scanf("%d", &t);eletm[t] = i;}tmque[i] = -1;}queue<int> emp;while(!que.empty())que.dequeue(emp);char order[10];int ele;while(scanf("%s", order)==1){if(!strcmp(order,"STOP") )break;else if(!strcmp(order,"ENQUEUE")){scanf("%d", &ele);if(tmque[eletm[ele] ] == -1){queue<int> q;q.enqueue(ele);que.enqueue(q);tmque[eletm[ele] ] = que.rear;}else{que.mem[tmque[eletm[ele] ] ].enqueue(ele);}}else if(!strcmp(order,"DEQUEUE")){int out;int id = que.front;que.mem[id].dequeue(out);if(que.mem[id].empty() ){tmque[eletm[out] ] = -1;que.dequeue(emp);}printf("%d\n", out);}}putchar('\n');}}

附上测试数据:

Input:

2
3 1 2 3
3 4 5 6
ENQUEUE 1
ENQUEUE 2
ENQUEUE 4
DEQUEUE
DEQUEUE
ENQUEUE 3
ENQUEUE 1
ENQUEUE 5
ENQUEUE 6
DEQUEUE
DEQUEUE
DEQUEUE
ENQUEUE 1
DEQUEUE
STOP
2
3 1 2 3
3 4 5 6
ENQUEUE 1
ENQUEUE 2
ENQUEUE 4
DEQUEUE
DEQUEUE
DEQUEUE
ENQUEUE 3
ENQUEUE 5
ENQUEUE 6
DEQUEUE
DEQUEUE
DEQUEUE
ENQUEUE 1
DEQUEUE
STOP
2
2 1 2
2 3 4
ENQUEUE 1
ENQUEUE 2
ENQUEUE 3
ENQUEUE 4
DEQUEUE
DEQUEUE
ENQUEUE 1
ENQUEUE 2
DEQUEUE
DEQUEUE
STOP
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
2
2 3 4
1 5
ENQUEUE 3
ENQUEUE 5
ENQUEUE 4
DEQUEUE
DEQUEUE
DEQUEUE
STOP
0

Output:

Scenario #1
1
2
4
5
6
3

Scenario #2
1
2
4
3
5
6
1

Scenario #3
1
2
3
4

Scenario #4
101
102
103
201
202
203

Scenario #5
259001
259002
259003
259004
259005
260001

Scenario #6
3
4
5


0 0
原创粉丝点击