[UVA-11995]I Can Guess the Data Structure!

题意

判断数据结构类型

解析

直接模拟就行

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e4 + 10;
bool isstack, isque, ispri;
struct Stack {
	int top, st[maxn];
	void clear() {
		memset(st, 0, sizeof(st));
		top = 0;
	}
	int pop() {
		if (top == 0) return -1;
		return st[top--];
	}
	void insert(int x) {
		st[++top] = x;
	}
}s;
priority_queue<int> q;
struct Queue {
	int que[maxn], tail, head;
	void clear() {
		tail = head = 0;
		memset(que, 0, sizeof(que));
	}
	void insert(int x) {
		que[++tail] = x;
	}
	int pop() {
		if (tail == head) return -1;
		return que[++head];
	}
}qa;

inline void Insert(int x) {
	s.insert(x), qa.insert(x), q.push(x);
}

inline void check(int x) {
	if (q.size() == 0 || q.top() != x) ispri = false;
	if (q.size()) q.pop();
	if (qa.pop() != x) isque = false;
	if (s.pop() != x) isstack = false;
}

inline void Output() {
	int cnt = (int)isstack + (int)ispri + (int)isque;
	if (cnt >= 2) {
		puts("not sure");
	} else if (cnt == 0) {
		puts("impossible");
	} else {
		if (isstack) {
			puts("stack");
		} else if (ispri) {
			puts("priority queue");
		} else {
			puts("queue");
		}
	}
}

int main() {
	int n;
	while (~scanf("%d", &n)) {
		s.clear(); qa.clear();
		while (q.size() > 0) q.pop();
		isstack = ispri = isque = true;
		for (int i = 1, opt, x; i <= n; ++ i) {
			scanf("%d %d", &opt, &x);
			if (opt & 1) {
				Insert(x);
			} else check(x);
		}
		Output();
	}
}
posted @ 2018-11-27 21:39  AlessandroChen  阅读(174)  评论(0编辑  收藏  举报