HDU 1387 Team Queue(优先队列)

Team Queue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1524    Accepted Submission(s): 515

Problem Description

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 will contain one or more test cases. Each test case begins with the number of teams t (1<=t<=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.

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

Source

University of Ulm Local Contest 1998

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
const int N = 1000005 ;
struct NODE
{
    int pri1 , pri2 , id ;
    friend bool operator<(NODE aa , NODE bb){
        if(aa.pri1 == bb.pri1)
            return aa.pri2>bb.pri2 ;
        return aa.pri1 > bb.pri1 ;
    }
};
int pri[1005] , num[1005] ;
int team[N] ;
void work()
{
    priority_queue<NODE> q;
    NODE now ;
    int id  , k =0 ;
    char str[100];
    memset(num , 0 ,sizeof(num)) ;
    memset(pri , -1 , sizeof(pri)) ;

    while(scanf("%s",str) > 0){
        if(str[0] == 'S')
            break;
        if(str[0] == 'E')
        {
            scanf("%d",&id);
            now.id = id ;
            now.pri2 = k ;
            if(team[id] == -1 )
                now.pri1 = k  ;
            else if(pri[ team[id] ] == -1 ){
                now.pri1 = k ;
                pri[ team[id] ] = k ;
                num[ team[id] ]++;
            }
            else now.pri1 =  pri[ team[id] ] , num[ team[id] ]++;;

            q.push( now ) ; k++;
        }
        else{
            now = q.top() ; q.pop() ;
            num[ team[now.id] ]--;
            if(num[ team[now.id] ]==0)
                pri[ team[now.id] ] = -1 ;

            printf("%d\n",now.id) ;
        }
    }
    printf("\n");
}
int main()
{
    int t , n , id , _cas = 0;

    while(scanf("%d",&t)>0 && t ){
        memset( team , -1 , sizeof(team));
        for(int i = 0 ; i < t ; i++)
        {
            scanf("%d",&n) ;
            while(n--){
                scanf("%d",&id);
                team[id] = i ;
            }
        }
        printf("Scenario #%d\n",++_cas) ;
        work();
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-09 03:04:27

HDU 1387 Team Queue(优先队列)的相关文章

HDU 1387 Team Queue

Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1889    Accepted Submission(s): 639 Problem Description Queues and Priority Queues are data structures which are known to most computer

HDU 1387 Team Queue( 单向链表 )

Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1294    Accepted Submission(s): 442 Problem Description Queues and Priority Queues are data structures which are known to most computer

hdu 1387 Team Queue (链表)

题目大意: 不同的人在不同的队伍里,插入链表的时候假设这个链表里有他的队友,就把它放到最后一个队友的最后.假设没有队友,就把它放到整个链表的最后面. 出链表的时候把第一个人拿出来. 思路分析: 要模拟这个链表就要记录这整个链表中的队伍存在的情况. 所以要再开一个链表的头指针和尾指针,在整个大的链表中再模拟小区域的链表. 然后就是deque部分,也就是注意head的推断以及更新. #include <cstdio> #include <iostream> #include <a

Team Queue(STL练习题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1387 Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1518    Accepted Submission(s): 511 Problem Description Queues and Priority Queues

hdu 1387(Team Queue) STL

Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2051    Accepted Submission(s): 713 Problem Description Queues and Priority Queues are data structures which are known to most computer

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

HDU 1242——Rescue(优先队列)

题意: 一个天使a被关在迷宫里,她的许多小伙伴r打算去救她,求小伙伴就到她需要的最小时间.在迷宫里有守卫,打败守卫需要一个单位时间,如果碰到守卫必须要杀死他 思路: 天使只有一个,她的小伙伴有很多,所以可以让天使找她的小伙伴,一旦找到小伙伴就renturn.时间小的优先级高.优先队列搞定 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<

hdu 2850 Load Balancing (优先队列 + 贪心)

题目大意: 怎么分配n个任务到m个服务器上使得负载尽量平衡. 思路: 将任务从大到小排序,依次放入负载最小的那个服务器中. 因为是spj 的缘故,所以可以使用这个贪心. 比如数据 6 2 7 5 3 3 3 3 就会得到错误答案. #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <queue> using namespac

HDU 4857 逃生 (优先队列+反向拓扑)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 解题报告:有n个点,有m个条件限制,限制是像这样的,输入a  b,表示a必须排在b的前面,如果不能确定两个数谁排在前面则尽量把小的排在前面. 首先把出度为0的点加入到优先队列中,然后每次用优先队列中弹出的点去更新其它点的出度,更新的同时如果又有其它点的出度为0的话又加到优先队列中, 最后按照从优先队列中出队的反序输出就可以了.我还是不懂为什么按照入度为0然后加入到优先队列然后正序输出这样为什么