24h購物| | PChome| 登入
2012-02-23 15:45:09| 人氣2,341| 回應0 | 上一篇 | 下一篇

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

推薦 0 收藏 0 轉貼0 訂閱站台

Problem I

I Can Guess the Data Structure!

There is a bag-like data structure, supporting two operations:

1 x

Throw an element x into the bag.

2

Take out an element from the bag.

Given a sequence of operations with return values, you're going to guess the data structure. It is a stack (Last-In, First-Out), a queue (First-In, First-Out), a priority-queue (Always take out larger elements first) or something else that you can hardly imagine!

Input

There are several test cases. Each test case begins with a line containing a single integer n (1<=n<=1000). Each of the next n lines is either a type-1 command, or an integer 2 followed by an integer x. That means after executing a type-2 command, we get an element x without error. The value of x is always a positive integer not larger than 100. The input is terminated by end-of-file (EOF). The size of input file does not exceed 1MB.

Output

For each test case, output one of the following:

stack

It's definitely a stack.

queue

It's definitely a queue.

priority queue

It's definitely a priority queue.

impossible

It can't be a stack, a queue or a priority queue.

not sure

It can be more than one of the three data structures mentioned above.

Sample Input

6
1 1
1 2
1 3
2 1
2 2
2 3
6
1 1
1 2
1 3
2 3
2 2
2 1
2
1 1
2 2
4
1 2
1 1
2 1
2 2
7
1 2
1 5
1 1
1 3
2 5
1 4
2 4

Output for the Sample Input

queue
not sure
impossible
stack
priority queue




#include<stdio.h>
int cmd[1000], x[1000];
int testStack(int n) {
int Stack[1005], idx = -1;
int i;
for(i = 0; i < n; i++) {
if(cmd[i] == 1) {
Stack[++idx] = x[i];
} else {
if(idx < 0 || Stack[idx] != x[i])
return 0;
--idx;
}
}
return 1;
}
int testQueue(int n) {
int Queue[1005], head = 0, tail = -1;
int i;
for(i = 0; i < n; i++) {
if(cmd[i] == 1)
Queue[++tail] = x[i];
else {
if(head > tail || Queue[head] != x[i])
return 0;
++head;
}
}
return 1;
}
int testPriorityQueue(int n) {
int Heap[1005], L = 0, P, S, tmp; /*max-heap*/
int i;
for(i = 0; i < n; i++) {
if(cmd[i] == 1) {
Heap[++L] = x[i];
S = L, P = S>>1;
while(S >= 2 && Heap[S] > Heap[P]) {
tmp = Heap[S], Heap[S] = Heap[P], Heap[P] = tmp;
S = P, P = S>>1;
}
} else {
if(L < 1 || Heap[1] != x[i])
return 0;
tmp = Heap[L], Heap[L] = Heap[1], Heap[1] = tmp;
P = 1, S = P<<1, --L;
while(S <= L) {
if(S < L && Heap[S+1] > Heap[S])
S++;
if(Heap[S] <= Heap[P])
break;
tmp = Heap[S], Heap[S] = Heap[P], Heap[P] = tmp;
P = S, S = P<<1;
}
}
}
return 1;
}
int main() {
int n, i;
while(scanf("%d", &n) == 1) {
for(i = 0; i < n; i++)
scanf("%d %d", &cmd[i], &x[i]);
int flag = 0, ans;
if(testStack(n))
ans = 1, flag++;
if(testQueue(n))
ans = 2, flag++;
if(testPriorityQueue(n))
ans = 3, flag++;
if(flag == 0)
puts("impossible");
else if(flag == 1) {
if(ans == 1)
puts("stack");
else if(ans == 2)
puts("queue");
else
puts("priority queue");
} else {
puts("not sure");
}
}
return 0;
}

台長: Morris
人氣(2,341) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 1208 - Oreon
此分類上一篇:[UVA] 11888 - Abnormal 89's

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文