質問編集履歴

1

解決

2018/11/11 18:59

投稿

opyon
opyon

スコア1009

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,311 @@
1
+ ###解決!(タイプミスでした^^;)
2
+
3
+ ```C++
4
+
5
+ #include <bits/stdc++.h>
6
+
7
+
8
+
9
+ class CBTree
10
+
11
+ {
12
+
13
+ private:
14
+
15
+ int heap_size = 0;
16
+
17
+ const int lowerlimit = -1;
18
+
19
+ std::vector<int> heap;
20
+
21
+
22
+
23
+ void print_key_()
24
+
25
+ {
26
+
27
+ for (int i = 1; i <= heap_size; ++i)
28
+
29
+ {
30
+
31
+ std::cout << " " << heap[i];
32
+
33
+ }
34
+
35
+ std::cout << "\n";
36
+
37
+ }
38
+
39
+
40
+
41
+ int node_L(const int &idx)
42
+
43
+ {
44
+
45
+ if (idx * 2 <= heap_size)
46
+
47
+ {
48
+
49
+ return idx * 2;
50
+
51
+ }
52
+
53
+ return 0;
54
+
55
+ }
56
+
57
+
58
+
59
+ int node_R(const int &idx)
60
+
61
+ {
62
+
63
+ if (idx * 2 + 1 <= heap_size)
64
+
65
+ {
66
+
67
+ return idx * 2 + 1;
68
+
69
+ }
70
+
71
+ return 0;
72
+
73
+ }
74
+
75
+
76
+
77
+ void maxHeapify_(const int &x)
78
+
79
+ {
80
+
81
+ int L = node_L(x);
82
+
83
+ int ans = heap[L] < heap[x] ? x : L;
84
+
85
+ if (heap_size >= 3)
86
+
87
+ {
88
+
89
+ int R = node_R(x);
90
+
91
+ ans = heap[ans] < heap[R] ? R : ans;
92
+
93
+ }
94
+
95
+ if (heap[ans] != heap[x])
96
+
97
+ {
98
+
99
+ std::swap(heap[ans], heap[x]);
100
+
101
+ maxHeapify_(ans);
102
+
103
+ }
104
+
105
+ }
106
+
107
+
108
+
109
+ public:
110
+
111
+ CBTree()
112
+
113
+ {
114
+
115
+ heap.resize(2000001, 0);
116
+
117
+ heap[0] = lowerlimit;
118
+
119
+ }
120
+
121
+
122
+
123
+ void push(int key)
124
+
125
+ {
126
+
127
+ heap[++heap_size] = key;
128
+
129
+ if (heap[1] < key)
130
+
131
+ {
132
+
133
+ std::swap(heap[1], heap[heap_size]);
134
+
135
+ }
136
+
137
+
138
+
139
+ int i = heap_size;
140
+
141
+ while (i > 1 && heap[i / 2] < heap[i])
142
+
143
+ {
144
+
145
+ std::swap(heap[i / 2], heap[i]);
146
+
147
+ i /= 2;
148
+
149
+ }
150
+
151
+ }
152
+
153
+
154
+
155
+ int top()
156
+
157
+ {
158
+
159
+ return heap[1];
160
+
161
+ }
162
+
163
+ void pop()
164
+
165
+ {
166
+
167
+ heap[1] = heap[heap_size];
168
+
169
+ --heap_size;
170
+
171
+ maxHeapify_(1);
172
+
173
+ }
174
+
175
+ void print_key()
176
+
177
+ {
178
+
179
+ print_key_();
180
+
181
+ }
182
+
183
+ };
184
+
185
+
186
+
187
+ void alds1_9_3()
188
+
189
+ {
190
+
191
+ // 完全二分木
192
+
193
+ // Complete binary tree
194
+
195
+ // 優先順位キュー
196
+
197
+ // priority queue
198
+
199
+ CBTree T;
200
+
201
+
202
+
203
+ std::string s;
204
+
205
+ int key;
206
+
207
+ while (s != "end")
208
+
209
+ {
210
+
211
+ std::cin >> s;
212
+
213
+ if (s == "insert")
214
+
215
+ {
216
+
217
+ std::cin >> key;
218
+
219
+ T.push(key);
220
+
221
+ }
222
+
223
+ else if (s == "extract")
224
+
225
+ {
226
+
227
+ std::cout << T.top() << "\n";
228
+
229
+ T.pop();
230
+
231
+ }
232
+
233
+ // デバッグ用
234
+
235
+ // T.print_key();
236
+
237
+ }
238
+
239
+ }
240
+
241
+
242
+
243
+ int main()
244
+
245
+ {
246
+
247
+ if (0)
248
+
249
+ {
250
+
251
+ std::cin.tie(0);
252
+
253
+ std::ios::sync_with_stdio(false);
254
+
255
+ }
256
+
257
+ alds1_9_3();
258
+
259
+ getchar();
260
+
261
+ return 0;
262
+
263
+ }
264
+
265
+
266
+
267
+ // https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_9_C
268
+
269
+
270
+
271
+ // 入力例 1
272
+
273
+ // insert 8
274
+
275
+ // insert 2
276
+
277
+ // extract
278
+
279
+ // insert 10
280
+
281
+ // extract
282
+
283
+ // insert 11
284
+
285
+ // extract
286
+
287
+ // extract
288
+
289
+ // end
290
+
291
+
292
+
293
+ // 出力例 1
294
+
295
+ // 8
296
+
297
+ // 10
298
+
299
+ // 11
300
+
301
+ // 2
302
+
303
+ ```
304
+
305
+
306
+
307
+
308
+
1
309
  ###知りたいこと
2
310
 
3
311
  どこが間違っているのかご教示頂けると助かります。