Fork me on GitHub

Uva 540 - Team Queue

  Team Queue 

Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known, though it occurs often in everyday life. At lunch time the queue in front of the Mensa is a team queue, for example.

 


In a team queue each element belongs to a team. If an element enters the queue, it first searches the queue from head to tail to check if some of its teammates (elements of the same team) are already in the queue. If yes, it enters the queue right behind them. If not, it enters the queue at the tail and becomes the new last element (bad luck). Dequeuing is done like in normal queues: elements are processed from head to tail in the order they appear in the team queue.

 


Your task is to write a program that simulates such a team queue.

 

Input 

The input file will contain one or more test cases. Each test case begins with the number of teams t ($1 \le t \le 1000$). Then t team descriptions follow, each one consisting of the number of elements belonging to the team and the elements themselves. Elements are integers in the range 0 - 999999. A team may consist of up to 1000 elements.

Finally, a list of commands follows. There are three different kinds of commands:

 

  • ENQUEUE x - enter element x into the team queue
  • DEQUEUE - process the first element and remove it from the queue
  • STOP - end of test case

The input will be terminated by a value of 0 for t.

 


Warning: A test case may contain up to 200000 (two hundred thousand) commands, so the implementation of the team queue should be efficient: both enqueing and dequeuing of an element should only take constant time.

 

Output 

For each test case, first print a line saying ``Scenario #k", where k is the number of the test case. Then, for each DEQUEUE command, print the element which is dequeued on a single line. Print a blank line after each test case, even after the last one.

 

Sample Input 

 

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
0

 

Sample Output 

 

Scenario #1
101
102
103
201
202
203

Scenario #2
259001
259002
259003
259004
259005
260001

 

 


Miguel A. Revilla 
1999-01-11
 
#include<stdio.h>
#include<string.h>
#include<malloc.h>

typedef struct node
{//结构体:队伍team类型 
    int num;
    int elem[1010]; //以数字的形式存储 element 
    
}node;

typedef struct queue
{//结构体:存储整条队列,即排队的队形 
    int belong; //属于哪一team的(存储的是数组的下标) 
    char name[10]; //为防万一,用字符串类型存储 element 
    struct queue* next; //指向下一个元素(不知道哪一team的) 
}queue;


typedef struct address
{//结构体:存储的是地址,主要为插入元素服务 
    int num; // 在整条队列(queue)中所在team中的成员的数目 
    queue *point;//指向的是属于这个team中在队列(queue)中排在最后的元素 
}address;
//这个结构体主要是在TL后定义的,一开始一直遍历,而导致效率低了,而且题目也有提示 

node list[1010]; // 存储一开始输入的各种team 
address ads[1010]; // 存储插入的地址 

int cmp(const void *a, const void *b)
{//将team中元素排列的函数,主要是为了确认在queue插入元素时较快的确认是属于哪一team的 
    return *(int *)a > *(int *)b;
}

int found(int n, int digit)
{//找到插入queue中的元素是属于哪一team的,然后赋值到成员类型belong中 
    int i, j, flag;
    for(flag=i=0; i<n; ++i)
    {
        for(j=0; j<list[i].num; ++j)
        {
            if(list[i].elem[j] == digit)
            {
                flag = 1;
                break;
            }
            else if(list[i].elem[j] > digit) break;
        }
        if(flag) return i;
    }
    return 0;
}

int main()
{

    int T, i, j, cnt = 0, loc, digit, flag, k;
    char command[10], number[10];
    queue *line = (queue *)malloc(sizeof(queue)); // queue的结点,题目都是围绕这条队展开的 
    line->belong = 0, line->next = NULL;
    queue *str, *stp, *stq, *sts, *tail;
    while(scanf("%d", &T) != EOF && T != 0)
    {
        flag = 1;
        tail = str = line;
        stp = stq = sts = str->next;
        for(i=0; i<T; ++i)
        {//输入开始要存储的信息 
            ads[i].num = 0, ads[i].point = NULL;
            scanf("%d", &list[i].num);
            for(j=0; j<list[i].num; ++j)
            scanf("%d", &list[i].elem[j]);
            qsort(list[i].elem, list[i].num, sizeof(int), cmp);
        }
        while(scanf("%s", command) != EOF && strcmp(command, "STOP") != 0)
        {//各种command,直到遇到"STOP"结束 
            if(strcmp(command, "ENQUEUE") == 0)
            {
                scanf("%s", number);
                digit = atoi(number);
                loc = found(T, digit); // 找到属于哪个team的编号 
                if(ads[loc].num == 0)
                {//queue中没有这种元素时 
                    stq = (queue*)malloc(sizeof(queue));
                    stq->belong = loc;
                    strcpy(stq->name, number);
                    stq->next = NULL;
                    tail->next = stq;
                    tail = stq; // 这里或者以后遇到的tail不容小觑,因为当你插入在queue没有team的新元素时,
                                // 你必须将它插入最后,这时就要用到tail 
                    
                    ads[loc].num++;
                    ads[loc].point = stq;
                }
                else 
                {// point 一直都指向在queue中属于它那team的最后一个元素,这样再插入一个同team的元素时就可以
                // 很方便地找到它,而且复杂度为O(1) 
                    stq = (queue*)malloc(sizeof(queue));
                    stq->belong = loc;
                    strcpy(stq->name, number);
                    stq->next = NULL;
                    
                    stp = ads[loc].point->next;
                    if(stp == NULL) tail = stq; //都说要注意tail 
                    ads[loc].point->next = stq;
                    stq->next = stp;
                    ads[loc].point = stq;
                    
                    ads[loc].num++;
                }
                
            }
            else if(strcmp(command, "DEQUEUE") == 0)
            {
                if(flag == 1) 
                {
                    printf("Scenario #%d\n", ++cnt);
                    flag = 0;
                }
                if((stp = line->next) != NULL)
                {//这里是输出头元素 
                    printf("%s\n", stp->name);
                    stp = stp->next;
                    k = line->next->belong;
                    ads[k].num--;
                    if(ads[k].num == 0) ads[k].point = NULL;
                    free(line->next);
                    line->next = stp;
                }
            }
        }
        str = line;
        stp = str = str->next;
        while(str != NULL)
        {//清除元素 
            stp = str->next;
            free(str);
            str = stp;
        }
        line->next = NULL;
        printf("\n");
    }
    return 0;
}

解题思路:

看代码的注释

Wrong answer的原因竟然是在最后多输出了一个换行,

posted @ 2013-03-19 13:29  Gifur  阅读(720)  评论(0编辑  收藏  举报
TOP