-
Notifications
You must be signed in to change notification settings - Fork 27
/
11995.go
116 lines (102 loc) · 2.16 KB
/
11995.go
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// UVa 11995 - I Can Guess the Data Structure!
package main
import (
"container/heap"
"fmt"
"os"
)
type (
operation struct{ op, x int }
node struct{ x int }
priorityQueue []*node
)
func (pq priorityQueue) Len() int { return len(pq) }
func (pq priorityQueue) Less(i, j int) bool { return pq[i].x > pq[j].x }
func (pq priorityQueue) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] }
func (pq *priorityQueue) Push(x interface{}) { *pq = append(*pq, x.(*node)) }
func (pq *priorityQueue) Pop() interface{} {
item := (*pq)[pq.Len()-1]
*pq = (*pq)[0 : pq.Len()-1]
return item
}
func isQueue(operations []operation) bool {
var queue []int
for _, o := range operations {
if o.op == 1 {
queue = append(queue, o.x)
} else {
if len(queue) == 0 {
return false
}
if queue[0] != o.x {
return false
}
queue = queue[1:]
}
}
return true
}
func isStack(operations []operation) bool {
var stack []int
for _, o := range operations {
if o.op == 1 {
stack = append([]int{o.x}, stack...)
} else {
if len(stack) == 0 {
return false
}
if stack[0] != o.x {
return false
}
stack = stack[1:]
}
}
return true
}
func isPriorityQueue(operations []operation) bool {
var pq priorityQueue
for _, o := range operations {
if o.op == 1 {
heap.Push(&pq, &node{o.x})
} else {
if pq.Len() == 0 {
return false
}
if curr := heap.Pop(&pq).(*node); curr.x != o.x {
return false
}
}
}
return true
}
func solve(operations []operation) string {
switch q, s, pq := isQueue(operations), isStack(operations), isPriorityQueue(operations); {
case q && !s && !pq:
return "queue"
case !q && s && !pq:
return "stack"
case !q && !s && pq:
return "priority queue"
case !q && !s && !pq:
return "impossible"
default:
return "not sure"
}
}
func main() {
in, _ := os.Open("11995.in")
defer in.Close()
out, _ := os.Create("11995.out")
defer out.Close()
var n int
for {
if _, err := fmt.Fscanf(in, "%d", &n); err != nil {
break
}
operations := make([]operation, n)
for i := range operations {
fmt.Fscanf(in, "%d%d", &operations[i].op, &operations[i].x)
}
fmt.Fprintln(out, solve(operations))
}
}