forked from int28h/JavaTasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityqueue.java
More file actions
108 lines (94 loc) · 2.64 KB
/
priorityqueue.java
File metadata and controls
108 lines (94 loc) · 2.64 KB
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
/**
* Первая строка входа содержит число операций 1 <= n <= 10^5.
* Каждая из последующих n строк задают операцию одного из
* следующих двух типов:
* Insert x, где 0 <= x <= 10^9 — целое число;
* ExtractMax.
*
* Первая операция добавляет число x в очередь с приоритетами,
* вторая — извлекает максимальное число и выводит его.
*
* Sample Input:
* 6
* Insert 200
* Insert 10
* ExtractMax
* Insert 5
* Insert 500
* ExtractMax
*
* Sample Output:
* 200
* 500
*/
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class PriorQueue {
private int heapSize = 0;
private List<Integer> heap = new LinkedList<Integer>();
private void run() {
Scanner sc = new Scanner(System.in);
int commandsCount = sc.nextInt();
commandsCount++;
String[] commands = new String[commandsCount];
for (int i = 0; i < commandsCount; i++) {
commands[i] = sc.nextLine();
}
for (String command : commands) {
if (command.startsWith("Insert")) {
String[] commandSplit = command.split(" ");
add(Integer.parseInt(commandSplit[1]));
} else if (command.startsWith("ExtractMax")) {
System.out.println(getMax());
}
}
}
private int getMax() {
int result = heap.get(0);
heapSize--;
heap.set(0, heap.get(heapSize));
heap.remove(heapSize);
heapify(0);
return result;
}
private void heapify(int i) {
int leftChild;
int rightChild;
int largestChild;
for (; ; ) {
leftChild = 2 * i + 1;
rightChild = 2 * i + 2;
largestChild = i;
if (leftChild < heapSize && heap.get(leftChild) > heap.get(largestChild)) {
largestChild = leftChild;
}
if (rightChild < heapSize && heap.get(rightChild) > heap.get(largestChild)) {
largestChild = rightChild;
}
if (largestChild == i) {
break;
}
int temp = heap.get(i);
heap.set(i, heap.get(largestChild));
heap.set(largestChild, temp);
i = largestChild;
}
}
private void add(int x) {
heap.add(x);
heapSize++;
int i = heapSize - 1;
int parent = (i - 1) / 2;
while (i > 0 && heap.get(parent) < heap.get(i)) {
int temp = heap.get(i);
heap.set(i, heap.get(parent));
heap.set(parent, temp);
i = parent;
parent = (i - 1) / 2;
}
}
public static void main(String[] args) throws Exception {
new PriorQueue().run();
}
}