uva - 11995 I Can Guess the Data Structure! Solution
uva - 11995 I Can Guess the Data Structure!
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<queue>
#include<vector>
#include<functional>
#include<stack>
using namespace std;
int main(){
int arr[3];
stack<int> st;
queue<int> q;
priority_queue<int> pq;
int i, z, n, x, y;
while (cin >> n){
arr[0] = arr[1] = arr[2] = 0;
while (!st.empty())
st.pop();
while (!q.empty())
q.pop();
while (!pq.empty())
pq.pop();
for (i = 0; i < n; i++){
cin >> x >> y;
if (x == 1){
if (arr[0] != 2)
st.push(y);
if (arr[1] != 2)
q.push(y);
if (arr[2] != 2)
pq.push(y);
}
else{
if (!q.empty() && arr[1] != 2){
z = q.front();
if (z == y)
arr[1] = 1;
else
arr[1] = 2;
q.pop();
}
else{
arr[1] = 2;
}
if (!st.empty() && arr[0] != 2){
z = st.top();
if (z == y)
arr[0] = 1;
else
arr[0] = 2;
st.pop();
}
else{
arr[0] = 2;
}
if (!pq.empty() && arr[2] != 2){
z = pq.top();
if (z == y)
arr[2] = 1;
else
arr[2] = 2;
pq.pop();
}
else{
arr[2] = 2;
}
}
}
if (arr[0] == 2 && arr[1] == 2 && arr[2] == 2 )
cout << "impossible" << endl;
else if ((arr[0] == 1 && arr[1] == 1) || (arr[1] == 1 && arr[2] == 1) || (arr[2] == 1 && arr[0] == 1) || (arr[0] == 0 && arr[1] == 0 && arr[2] == 0))
cout << "not sure" << endl;
else if (arr[0] == 1)
cout << "stack" << endl;
else if (arr[1] == 1)
cout << "queue" << endl;
else
cout << "priority queue" << endl;
}
return 0;
}
Comments
Post a Comment