質問するログイン新規登録

質問編集履歴

1

解決

2018/11/11 18:59

投稿

opyon
opyon

スコア1009

title CHANGED
File without changes
body CHANGED
@@ -1,3 +1,157 @@
1
+ ###解決!(タイプミスでした^^;)
2
+ ```C++
3
+ #include <bits/stdc++.h>
4
+
5
+ class CBTree
6
+ {
7
+ private:
8
+ int heap_size = 0;
9
+ const int lowerlimit = -1;
10
+ std::vector<int> heap;
11
+
12
+ void print_key_()
13
+ {
14
+ for (int i = 1; i <= heap_size; ++i)
15
+ {
16
+ std::cout << " " << heap[i];
17
+ }
18
+ std::cout << "\n";
19
+ }
20
+
21
+ int node_L(const int &idx)
22
+ {
23
+ if (idx * 2 <= heap_size)
24
+ {
25
+ return idx * 2;
26
+ }
27
+ return 0;
28
+ }
29
+
30
+ int node_R(const int &idx)
31
+ {
32
+ if (idx * 2 + 1 <= heap_size)
33
+ {
34
+ return idx * 2 + 1;
35
+ }
36
+ return 0;
37
+ }
38
+
39
+ void maxHeapify_(const int &x)
40
+ {
41
+ int L = node_L(x);
42
+ int ans = heap[L] < heap[x] ? x : L;
43
+ if (heap_size >= 3)
44
+ {
45
+ int R = node_R(x);
46
+ ans = heap[ans] < heap[R] ? R : ans;
47
+ }
48
+ if (heap[ans] != heap[x])
49
+ {
50
+ std::swap(heap[ans], heap[x]);
51
+ maxHeapify_(ans);
52
+ }
53
+ }
54
+
55
+ public:
56
+ CBTree()
57
+ {
58
+ heap.resize(2000001, 0);
59
+ heap[0] = lowerlimit;
60
+ }
61
+
62
+ void push(int key)
63
+ {
64
+ heap[++heap_size] = key;
65
+ if (heap[1] < key)
66
+ {
67
+ std::swap(heap[1], heap[heap_size]);
68
+ }
69
+
70
+ int i = heap_size;
71
+ while (i > 1 && heap[i / 2] < heap[i])
72
+ {
73
+ std::swap(heap[i / 2], heap[i]);
74
+ i /= 2;
75
+ }
76
+ }
77
+
78
+ int top()
79
+ {
80
+ return heap[1];
81
+ }
82
+ void pop()
83
+ {
84
+ heap[1] = heap[heap_size];
85
+ --heap_size;
86
+ maxHeapify_(1);
87
+ }
88
+ void print_key()
89
+ {
90
+ print_key_();
91
+ }
92
+ };
93
+
94
+ void alds1_9_3()
95
+ {
96
+ // 完全二分木
97
+ // Complete binary tree
98
+ // 優先順位キュー
99
+ // priority queue
100
+ CBTree T;
101
+
102
+ std::string s;
103
+ int key;
104
+ while (s != "end")
105
+ {
106
+ std::cin >> s;
107
+ if (s == "insert")
108
+ {
109
+ std::cin >> key;
110
+ T.push(key);
111
+ }
112
+ else if (s == "extract")
113
+ {
114
+ std::cout << T.top() << "\n";
115
+ T.pop();
116
+ }
117
+ // デバッグ用
118
+ // T.print_key();
119
+ }
120
+ }
121
+
122
+ int main()
123
+ {
124
+ if (0)
125
+ {
126
+ std::cin.tie(0);
127
+ std::ios::sync_with_stdio(false);
128
+ }
129
+ alds1_9_3();
130
+ getchar();
131
+ return 0;
132
+ }
133
+
134
+ // https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_9_C
135
+
136
+ // 入力例 1
137
+ // insert 8
138
+ // insert 2
139
+ // extract
140
+ // insert 10
141
+ // extract
142
+ // insert 11
143
+ // extract
144
+ // extract
145
+ // end
146
+
147
+ // 出力例 1
148
+ // 8
149
+ // 10
150
+ // 11
151
+ // 2
152
+ ```
153
+
154
+
1
155
  ###知りたいこと
2
156
  どこが間違っているのかご教示頂けると助かります。
3
157