Description
Existing oneOne-way, one-laneIn the tunnel, each vehicle enters from one end of the tunnel, and the other ends exit. Overtaking is not allowed.
The tunnel has certain limitations on the vehicle height. At any time, the administrator wants to know the maximum vehicle height in the tunnel.
Now, please maintain the vehicle entry and exit records for this tunnel and support querying the highest vehicle functions
Input
The first line only contains an integer, that is, the total number of times the height query and vehicle access operations n
The following n rows are operated n times in sequence. The format of each row is one of the following:
1. E x // a car with a height of x enters the tunnel (x is an integer) 2. D // a car leaves the tunnel 3. M // query the maximum vehicle height in the tunnel at this time
Output
If operations D and M are performed for a total of m times, output m rows for each D operation, output the height of the exit tunnel vehicle, and output the maximum height queried for each M operation.
Input example
9E 5E 6ME 2 MDMDM
Output example
665662
Restrictions
0 ≤ n ≤ 2,000,000
0 ≤ x ≤ 231-1
Ensure that the vehicle's inbound and outbound sequence is legal
Prompt
How to simulate a queue with multiple stacks? See an exercise at the end of chapter 4.
How to implement a stack that can efficiently obtain the maximum value?
How to implement a queue that can efficiently obtain the maximum value?
For details, refer to the handouts in Chapter XA and [10-19] and [10-20] in exercise analysis.
[Solution]
The key to this question is how to maintain a queue that can query the maximum value of the current range with less time complexity.
A dual-end queue or a queue heap is required ).
Consider how to maintain a getMax () interface for the stack:
Through the above analysis, it is not difficult to write the following AC code (S and P are maintained by the linked list ):
1 # include <stdio. h> 2 # include <stdlib. h> 3 4 typedef struct Node 5 {6 int data; 7 struct Node * next, * pre; 8} node, * pnode; 9 10 typedef struct Count11 {12 int data, num; 13 struct Count * next, * pre; 14} count, * pcount; 15 16 int main (void) 17 {18 pnode shead = (pnode) malloc (sizeof (node); 19 pnode tmp1, stail = shead; 20 21 pcount phead = (pcount) malloc (sizeof (count); 22 pcount tmp2, ptail = phead; 23 24 int n; 25 26 scanf ("% d", & n); 27 28 for (int I = 0; I <n; ++ I) 29 {30 char ch; 31 int x, a; 32 33 do34 {35 ch = getchar (); 36} while (ch! = 'E') & (ch! = 'M') & (ch! = 'D'); 37 38 switch (ch) 39 {40 case 'e': 41 scanf ("% D", & x ); 42 43 // x into s44 tmp1 = (pnode) malloc (sizeof (node); 45 tmp1-> data = x; 46 stail-> next = tmp1; tmp1-> pre = stail; stail = tmp1; 47 48 // prepare num for x into p49 a = 1; 50 tmp2 = (pcount) malloc (sizeof (count )); 51 ptail-> next = tmp2; tmp2-> pre = ptail; ptail = tmp2; 52 while (ptail-> pre! = Phead) & (ptail-> pre-> data <= x) 53 {54 a + = ptail-> pre-> num; 55 56 tmp2 = ptail-> pre; 57 tmp2-> pre-> next = ptail; 58 ptail-> pre = tmp2-> pre; 59 delete tmp2; 60} 61 62 // x into p63 ptail-> data = x; ptail-> num = a; 64 65 break; 66 case 'D ': 67 printf ("% d \ n", shead-> next-> data); 68 shead = shead-> next; 69 delete shead-> pre; 70 71 if (! (-- (Phead-> next)-> num) 72 {73 phead = phead-> next; 74 delete phead-> pre; 75} 76 break; 77 case 'm': 78 printf ("% d \ n", phead-> next-> data); 79 break; 80} 81} 82 83 return 0; 84}
* Image parsing is from the exercise analysis of xuetangx data structure course by Ding Junhui
[Tsinghua OJ] tunneling (KNN) problem