Corresponding POJ topic: Click to open link
Double Queue
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 11768 |
|
Accepted: 5349 |
Description
The new founded Balkan Investment Group Bank (Big-bank) opened a new office in Bucharest, equipped with a modern comput ing environment provided by IBM Romania, and using modern information technologies. As usual, each client of the bank was identified by a positive integer K and, upon arriving to the bank For some services, he or she receives a positive integer priority P . One of the inventions of the young managers of the bank shocked the software engineer of the serving system. They proposed to breaks the tradition by sometimes calling the serving desk with the lowest prio Rity instead of that and the highest priority. Thus, the system would receive the following types of request:
| 0 |
The system needs to stop serving |
| 1 K P |
ADD client K to the waiting list with priority P |
| 2 |
Serve the client with the highest priority and drop him or she from the waiting list |
| 3 |
Serve the client with the lowest priority and drop him or she from the waiting list |
Your task is to help the software engineer of the bank by writing a program to implement the requested serving policy.
Input
Each line of the input contains one of the possible requests; The last line contains the Stop-request (code 0). You may assume this when there are a request to include a new client in the list (code 1), there is no other request in the List of the same client or with the same priority. An identifier K was always less than 106, and a priority P was less than 107. The client may arrive to being served multiple times, and each time could obtain a different priority.
Output
For each request with code 2 or 3, the program have to print, in a separate line of the standard output, the identifier of The served client. If the request arrives when the waiting list is empty and then the program prints Zero (0) to the output.
Sample Input
21 20 141 30 321 10 993220
Sample Output
02030100
Test instructions
There are 3 actions: (1 x Y) to insert a pair of numbers (x y) in the queue with a priority of Y, where each logarithm has a different x and Y, and (2) an x in a pair with the highest output priority, and (3) an X in a pair of the lowest output priority, or (0) for exit.
Ideas:
A variety of binary sorting trees, as well as the STL Map,set can also be AC, this problem has been done rotten ~, put down the jumping table.
Skip table Knowledge (click to open link)
#include <cstdio> #include <cstdlib> #include <cmath> #include <ctime> #include <iostream >const int maxn=1000+10;using namespace Std;void inittime () {static int first = 1;IF (first) {first = 0;srand ((unsigned) t IME (0));}} struct Node{int V, K; Node *next, *down;}; Class Skiplist{public:node *newlay ()//create a new Layer {node *s = (node *) malloc (sizeof (node)); Node *e = (node *) malloc (sizeof (node)); s->k =-inf;e->k = Inf;s->next = E;e->next = Null;s->down = E->do WN = Null;return s;} void Init () {layer = 1;inf = 0x7fffffff;max_k =-inf;min_k = Inf;top = Findlay[layer] = Newlay ();} int Getlay ()//By rule Select which layer the data should be inserted into {int lay = 1;while (rand ()%2 && lay <= layer) Lay++;return lay;} void Insert (int a, int b) {if (b > Max_k) max_k = b;if (b < min_k) Min_k = b; Node *p, *s, *s1;int lay = Getlay (), if (Lay > Layer) {//layer to insert > number of existing layers P = newlay (); Findlay[++layer] = p; Mark Layer Number P->down = Top;top = P;} p = Findlay[lay]; Get the layer head pointer directly from the layer number bool OK = 0;while (p)//insert element into a column {while (P->k < b && P->next->k < b) p = P->next;s = (node *) malloc (sizeof (node)); s->v = a;s->k = b; S->next = P->next;s->down = Null;p->next = S;p = P->down;if (ok) s1->down = S;S1 = S;if (!ok) ok = 1;}} void Delete (BOOL flag)//flag = = 1 means delete maximum value {int d;if (flag) d = max_k;else D = min_k;if (-inf = = d | | inf = = d) {//description not yet inserted element PR intf ("0\n"); return;} Node *p = Top;while (p) {while (P->next->k < d) p = p->next;if (p->next->k = d) {if (NULL = = P->down) {//arrival Lowest level printf ("%d\n", p->next->v); if (-inf = = p->k && inf = p->next->next->k) {//To remove the last element, min _k and Max_k to special treatment max_k =-inf; Min_k = INF;} ELSE{IF (flag) Max_k = P->k;else min_k = p->next->next->k;}} Node *tmp = P->next;p->next = Tmp->next;free (TMP);} p = P->down;}} void Show () {Node *p = findlay[1];p = P->next;while (p->k! = inf) {printf ("%d", p->k);p = P->next;} printf ("\ n");} void free () {Node *p, *q;int i;for (i = 1; I <= layer; i++) {p = Findlay[i];while (p) {q = P->next;free (p);p = q;}}} Private:node *top, *findlay[70];int layer, INF, Max_k, Min_k;}; Skiplist Sl;int Main () {//freopen ("In.txt", "R", stdin); Inittime (); sl. Init (); int op, A, B;while (scanf ("%d", &op), op) {if (1 = = op) {scanf ("%d%d", &a, &b), sl. Insert (A, b);} else if (2 = = OP) sl. Delete (1); else sl. Delete (0);} Qll Free (); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Jumping table Base--poj 3481 Double Queue