1. 程式人生 > >UVa 11995 - I Can Guess the Data Structure!

UVa 11995 - I Can Guess the Data Structure!

spa 實現 size end amp ins post bool ret

題目:給你一些數據結構上的操作,推斷該數據結構是棧、隊列、還是優先隊列。

分析:0基礎DS,模擬。構建三種結構,直接模擬,然後依據結果推斷。

說明:優先隊列用最大堆實現。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

//stack
class stack
{
	private:  
        int  top;  
        int  keys[1005];
	public:
		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

//queue
class queue
{
	private:  
        int  move,save;  
        int  keys[1005];
	public:
		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

//heap
class 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 test
		int 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 test
		int 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 test
		int 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!