首页 > 代码库 > UVa 11995 - I Can Guess the Data Structure!

UVa 11995 - I Can Guess the Data Structure!

题目:给你一些数据结构上的操作,推断该数据结构是栈、队列、还是优先队列。

分析: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!