Introduction to Algorithms Classic-fifth chapter 5-6 Group queue

Source: Internet
Author: User

Topic background

Queues and priority queues are known to most computer scientists 数据结构 . But team queues are not well known, though they often occur in life. For example, the queue at the lunch-time cafeteria door is a team queue. In a team queue, each element belongs to a team. If an element enters a queue, it first searches the queue from start to finish-checking whether its teammates (called teammates in the same team) are in this queue. If there is, it is in the back of his teammates (:-D is to jump in line ~ ~). If not, it will be at the end of the queue and the new Last (/(ㄒoㄒ)/~ is unfortunate. In the normal queue, this is the case: elements are processed from beginning to end, in the order in which they appear in the team queue. Your task is to write a program that simulates such a team queue.

Input

The input file contains one or more test examples. Each test sample is started by the number of Mission teams T (1<=t<=1000). Then the T-team is described as follows, each team consists of a number representing the number of elements, and each element. The elements are integral and range between 0 and 999999 (1 million minus one). A team may have as many as 1000 elements. Finally, the instruction list is as follows. There are three different instructions: ENQUEUE x--x into the team queue. DEQUEUE x--handles the first element and removes it stop--the end of a test sample. When T is 0 o'clock, the input terminates. Warning: A test sample may have as many as 200000 (/(ㄒoㄒ)/~~ 200,000) instructions, so the implementation of Team queues should be efficient: both the queue and the team should spend constant time.

Output

For each test sample, first output a line of "Scenario #k", where K represents the first few tests. Then, each "DEQUEUE" instruction prints the element that contains the team (one line alone). Print a blank line after each test sample, even the last Test example.
For example:
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

Analysis

The topic clearly tells us to use the queue. Use a queue to arrange the team, and then use a queue to arrange the elements. The title says that the team can only spend constant time, so you have to establish a mapping between elements and teams and not simply use arrays to save elements.

#include <cstdio> #include <queue> #include <map> using namespace std;  const int MAXT = 1000 + 10;      int main () {int T, Kase = 0;            while (scanf ("%d", &t) = = 1 && t) {printf ("Scenario #%d\n", ++kase);              Map<int, int> team;//mapping function is the number x corresponding to its troop I for (int i = 0; i < T; i++) {int n,x;              scanf ("%d", &n);          while (n--) {scanf ("%d", &x); team[x] = i;}          } queue<int> Q, Q2[maxt]; The two queues are the core of the subject//q is the team, the Q2 is stored by the order of all the ranks of the queue and the number of the team//that is, Q storage team Overall queue, example {3,1,2}//Q2 Storage Team queue, example {103,101,102 },{201},{301,303} for (;;)          {int x;          Char cmd[10];          scanf ("%s", cmd);              if (cmd[0] = = ' S ') break;//encounters a stop Stop else if (cmd[0] = = ' D ') {int t = Q.front ();//use variable T to represent the team head of the overall queue printf ("%d\n", Q2[t].front ()); Q2[t].pop ();//outputs the first person in the team's first team and then puts the person out of the team if (Q2[t].empty ()) Q.pop ();//If the teamIn the entire queue only one person, then Q's team first out of the team, that is, the team out of the team}else if (cmd[0] = = ' E ') {scanf ("%d", &x); int t = team[x];//by map to find the queue ordinal if (Q2[t].empty ()) Q.push (t) of X, and/or if the team has not yet been queued, the queue is inserted at the end of the team Q2[t].push (x      );//Insert the person of the team into the team in Q2}} printf ("\ n");  } return 0; }

  

Push () will place an element into the queue.

    • Front () returns the first element within the queue (that is, the first element to be placed).
    • Back () returns the last element in the queue (that is, the element that was last inserted).
    • Top () takes the first element of the queue (but does not delete it).
    • Pop () removes an element from a queue.
    • Note: Pop () removes the next element, but does not return it, front () and back () return the next element without removing the element.

Other:

The priority queue is also defined in the header file, declared with "Priority_quene PQ". (The smaller the integer priority is the lower)

Since the elements of the team are not the elements of the most advanced team, the way out of the team has changed from front () to top ().

The smaller the integer precedence the greater the definition of "Priority_queue<int,vector,greater > PQ"

Custom types can also form a priority queue, but you must define a priority for each element.

eg. Implementing "single-digit integer precedence is small", you can define a struct CMP, overload the "()" operator, and then define it as "priority_queue<int,vector,cmp> PQ".

Here is the definition of CMP

struct cmp{

 bool operator() (const int a, const int b)const{ //a的优先级比b小时返回true return a%10>b%10; }

}

Introduction to Algorithms Classic-fifth chapter 5-6 Group queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.