AcWing Group Queue

Posted by JukEboX on Sun, 04 Aug 2019 19:39:30 +0200

AcWing Group Queue

Description

  • There are n groups in a queue, with several people in each group.

    When a person comes to the queue, if there are already members of his own group in the queue, he will jump directly behind the members of his own group, otherwise he will stand at the end of the queue.

    Please write a program to simulate this group queue.

Input

  • The input will contain one or more test cases.

    For each test case, enter the number of groups t in the first line.

    Next t lines, enter a group description for each line. The first number represents the number of people in the group, and the next number represents the number of people in the group.

    The number is an integer in the range 0 to 999999.

    A team can contain up to 1000 people.

    Finally, the list of commands is as follows.There are three different commands:

    1. ENQUEUE x - Insert a person whose number is x into the queue;

    2. DEQUEUE - Get the first person in the whole queue out of the queue;

    3. STOP - End of Test Case

    Each command takes one line.

    When input use case t=0, it means stop input.

    Note that test cases can contain up to 20,000 (200,000) commands, so the implementation of a team queue should be efficient:

    A constant time is required for both entry and exit.

Output

  • For each test case, a line "Scenario #k" is output first, where k is the number of the test case.

    Then, for each DEQUEUE command, the number of the person leaving the queue is output, one line for each number.

    After each test case (including the last test case) output is complete, an empty line is output.

Data Size

  • 1≤t≤1000

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

Questions:

  • Queue.
  • At a glance, it's probably a queue-related topic.The bottleneck is in the operation "If there is already a member of his own group in the queue, he will jump directly behind his own group."
  • But it's easy to imagine that you only need N queues and one total queue.In the total queue, there is a prioritization relationship between the groups; in the N queues, there are N groups, and in the group, there is a prioritization relationship among the members.Then you can get the light speed right!
#include <iostream>
#include <cstdio>
#include <queue>
#define N 1005
#define M 1000005
using namespace std;

int n, cnt;
int tem[M];
queue<int> mai, zero;
queue<int> a[N];

int read()
{
    int x = 0; char c = getchar();
    while(c < '0' || c > '9') c = getchar();
    while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    return x;
}

int main()
{
    while(scanf("%d", &n) == 1)
    {
        if(!n) break;
        printf("Scenario #%d\n", ++cnt);
        mai = zero;
        for(int i = 1; i <= n; i++) a[i] = zero;
        for(int i = 1; i <= n; i++)
        {
            int num = read();
            for(int j = 1; j <= num; j++)
                tem[read()] = i;
        }
        while(1)
        {
            char c[10]; scanf("%s", c);
            if(c[0] == 'E')
            {
                int x = read();
                if(!a[tem[x]].size()) mai.push(tem[x]);
                a[tem[x]].push(x);
            }
            else if(c[0] == 'D')
            {
                int id = mai.front();
                printf("%d\n", a[id].front());
                a[id].pop();
                if(!a[id].size()) mai.pop();
            }
            else if(c[0] == 'S') break;
        }
        printf("\n");
    }
    return 0;
}

Topics: PHP