zoukankan      html  css  js  c++  java
  • UVa 11995

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

    分析: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
    ");
    		else if (flags + flagq + flagh > 1)
    			printf("not sure
    ");
    		else if (flags)
    			printf("stack
    ");
    		else if (flagq)
    			printf("queue
    ");
    		else printf("priority queue
    ");
    	}
        return 0;
    }
    


  • 相关阅读:
    jquery load加载页面内ajax返回的div不能响应页面js的问题的解决方案
    JQuery跳出each循环的方法(包含数组遍历)
    fpm 配置详解
    curl模拟Http请求
    git 修改commit信息
    git-ssh 配置和使用
    初始化目录,并且添加到远程仓库
    【PHP代码审计】 那些年我们一起挖掘SQL注入
    【PHP代码审计】 那些年我们一起挖掘SQL注入
    【PHP代码审计】 那些年我们一起挖掘SQL注入
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/7069089.html
Copyright © 2011-2022 走看看