Title: give you some data structure operations, to determine whether the data structure is a stack, queue, or priority queue.
Analysis: Primary DS, simulation. Construct three structures, simulate directly and then judge by the results.
Description: The priority queue is implemented with the maximum heap.
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < Cstdio> #include <cmath>using namespace Std;//stackclass stack{private:int top; int keys[1005];p ublic:stack () {clear ();} bool Empty () {return top = = 0;} void Clear () {top = 0;} void Insert (int Key) {keys[top + +] = Key; } int Delete (void) {return keys[--top]; }};//stack end//queueclass Queue{private:int move,save; int keys[1005];p ublic:queue () {clear ();} bool Empty () {return move = = save;} void Clear () {move = save = 0;} void Insert (int Key) {Keys[save + +] = Key; } int Delete (void) {return keys[move + +]; }};//stack end//heapclass banery_heap{private:int size; int keys[1005]; Public:banery_heap () {clear ();} void Clear () {size = 0; } bool Empty () {return size = = 0;} void Insert (int Key) {keys[++ size] = Key; int now = size; while (now > 1 && keys[now] > keys[now>>1]) {swap (Keys[now], keys[now>>1]); now = now>>1; }} int Delete (void) {swap (keys[size], keys[1]); int now = 1; while (1) {int New = Now,l = (now<<1), R = (now<<1) +1; if (L < size && Keys[l] > keys[new]) New = l; if (R < size && Keys[r] > keys[new]) New = R; if (now = = New) break; Swap (Keys[now], keys[new]); now = New; } return Keys[size--]; } }; Heap End int A[1001],b[1001];int main () {stack s;queue q;banery_heap h;int n;while (~scanf ("%d", &n)) {for (int i = 0 ; I < n; + + i) scanf ("%d%d", &a[i],&b[i]);//stack Testint flags = 1; S.clear (); for (int i = 0; i < n; + + i) if (a[i] = = 1) s.insert (B[i]); else if (S.empty () | | | S.delete ()! = B[i]) {flags = 0;break;} Queue Testint flagq = 1; Q.clear (); for (int i = 0; i < n; + + i) if (a[i] = = 1) q.insert (B[i]); else if (Q.empty () | | | Q.delete ()! = B[i]) {flagq = 0;break;} Heap Testint Flagh = 1; H.clear (); for (int i = 0; i < n; + + i) if (a[i] = = 1) h.insert (B[i]); else if (H.empty () | | | H.delete ()! = B[i]) {Flagh = 0;break;} if (flags + flagq + Flagh = = 0) printf ("impossible\n"), else if (flags + FLAGQ + Flagh > 1) printf ("Not sure\n"); else if (flags) printf ("stack\n"), else if (FLAGQ) printf ("queue\n"), Else printf ("Priority queue\n");} return 0;}
UVa 11995-i Can Guess the Data structure!