-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuessDequePriorityQueue.java
80 lines (76 loc) · 2.16 KB
/
GuessDequePriorityQueue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3146
* #queue #stack #priority-queue provided that the data structure is used is
* queue/stack/priorityQueue(maxHeap).
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Scanner;
class GuessDequePriorityQueue {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
int n = sc.nextInt();
if (n == 0) {
break;
}
Deque<Integer> stack = new LinkedList<>();
Deque<Integer> queue = new LinkedList<>();
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
boolean stackFlg = true;
boolean queueFlg = true;
boolean maxHeapFlg = true;
for (int i = 0; i < n; i++) {
int command = sc.nextInt();
int val = sc.nextInt();
if (command == 1) {
if (stackFlg) {
stack.addLast(val);
}
if (queueFlg) {
queue.addLast(val);
}
if (maxHeapFlg) {
pq.add(val);
}
} else {
if (stackFlg && !stack.isEmpty() && stack.peekLast() == val) {
stack.pollLast();
} else {
stackFlg = false;
}
if (queueFlg && !queue.isEmpty() && queue.peekFirst() == val) {
queue.pollFirst();
} else {
queueFlg = false;
}
if (maxHeapFlg && !pq.isEmpty() && pq.peek() == val) {
pq.poll();
} else {
maxHeapFlg = false;
}
}
}
ArrayList<String> result = new ArrayList<>();
if (queueFlg) {
result.add("queue");
}
if (stackFlg) {
result.add("stack");
}
if (maxHeapFlg) {
result.add("priority queue");
}
if (result.size() == 0) {
System.out.println("impossible");
} else if (result.size() == 1) {
System.out.println(result.get(0));
} else {
System.out.println("not sure");
}
}
}
}