回答編集履歴
1
追記
test
CHANGED
@@ -161,3 +161,199 @@
|
|
161
161
|
[21,48] [29,49] [1,53] [6,54] [11,56] [25,60] [9,62] [0,63] [4,79] [28,94] [19,96] [8,97] [22,99] [20,99] [14,99]
|
162
162
|
|
163
163
|
```
|
164
|
+
|
165
|
+
**追記**
|
166
|
+
|
167
|
+
node の配列をソートしなければならないのに、質問のコードは
|
168
|
+
|
169
|
+
int の配列をソートしようとしています。修正してみました。
|
170
|
+
|
171
|
+
```C++
|
172
|
+
|
173
|
+
#include<iostream>
|
174
|
+
|
175
|
+
#include<random>
|
176
|
+
|
177
|
+
using namespace std;
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
#define N 30
|
182
|
+
|
183
|
+
#define MAX_COST 100
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
struct node {
|
188
|
+
|
189
|
+
int id;
|
190
|
+
|
191
|
+
int cost;
|
192
|
+
|
193
|
+
};
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
node node_data[N];
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
void gen_node_data()
|
202
|
+
|
203
|
+
{
|
204
|
+
|
205
|
+
mt19937 mt{random_device{}()};
|
206
|
+
|
207
|
+
uniform_int_distribution<int> dist(0,MAX_COST);
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
for (int i=0; i<N; i++) {
|
212
|
+
|
213
|
+
node_data[i].id = i;
|
214
|
+
|
215
|
+
node_data[i].cost = dist(mt);
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
void swap(node a[],int i,int j) // ★
|
224
|
+
|
225
|
+
{
|
226
|
+
|
227
|
+
node t; // ★
|
228
|
+
|
229
|
+
t=a[i];
|
230
|
+
|
231
|
+
a[i]=a[j];
|
232
|
+
|
233
|
+
a[j]=t;
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
void make_heap(node a[], int asize) // ★
|
240
|
+
|
241
|
+
{
|
242
|
+
|
243
|
+
for (int k = 0; ++k < asize; )
|
244
|
+
|
245
|
+
for (int i, j = k; j > 0; j = i) {
|
246
|
+
|
247
|
+
i = (j - 1) / 2;
|
248
|
+
|
249
|
+
if (a[i].cost <= a[j].cost) break;
|
250
|
+
|
251
|
+
swap(a, i, j);
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
bool check_rightmost(int i)
|
260
|
+
|
261
|
+
{
|
262
|
+
|
263
|
+
int x = i+2;
|
264
|
+
|
265
|
+
while (x>1) {
|
266
|
+
|
267
|
+
if (x%2==1) return 0;
|
268
|
+
|
269
|
+
else x=x/2;
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
return 1;
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
void print_heap()
|
280
|
+
|
281
|
+
{
|
282
|
+
|
283
|
+
for (int i=0; i<N; i++) {
|
284
|
+
|
285
|
+
cout << "id: ";
|
286
|
+
|
287
|
+
cout << node_data[i].id;
|
288
|
+
|
289
|
+
cout << ", cost: ";
|
290
|
+
|
291
|
+
cout << node_data[i].cost;
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
if (check_rightmost(i)) cout << endl;
|
296
|
+
|
297
|
+
else cout << " ";
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
cout << endl;
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
int main()
|
308
|
+
|
309
|
+
{
|
310
|
+
|
311
|
+
gen_node_data();
|
312
|
+
|
313
|
+
print_heap();
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
for (int i=0; i<N; i++) {
|
318
|
+
|
319
|
+
make_heap(&node_data[i], N-i); // ★
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
print_heap();
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
return 0;
|
328
|
+
|
329
|
+
}
|
330
|
+
|
331
|
+
```
|
332
|
+
|
333
|
+
実行結果
|
334
|
+
|
335
|
+
```text
|
336
|
+
|
337
|
+
id: 0, cost: 62
|
338
|
+
|
339
|
+
id: 1, cost: 29 id: 2, cost: 92
|
340
|
+
|
341
|
+
id: 3, cost: 13 id: 4, cost: 34 id: 5, cost: 38 id: 6, cost: 87
|
342
|
+
|
343
|
+
id: 7, cost: 71 id: 8, cost: 40 id: 9, cost: 7 id: 10, cost: 67 id: 11, cost: 68 id: 12, cost: 1 id: 13, cost: 67 id: 14, cost: 79
|
344
|
+
|
345
|
+
id: 15, cost: 68 id: 16, cost: 19 id: 17, cost: 31 id: 18, cost: 6 id: 19, cost: 73 id: 20, cost: 71 id: 21, cost: 93 id: 22, cost: 69 id: 23, cost: 65 id: 24, cost: 63 id: 25, cost: 1 id: 26, cost: 23 id: 27, cost: 84 id: 28, cost: 73 id: 29, cost: 1
|
346
|
+
|
347
|
+
id: 12, cost: 1
|
348
|
+
|
349
|
+
id: 25, cost: 1 id: 29, cost: 1
|
350
|
+
|
351
|
+
id: 18, cost: 6 id: 9, cost: 7 id: 3, cost: 13 id: 16, cost: 19
|
352
|
+
|
353
|
+
id: 26, cost: 23 id: 1, cost: 29 id: 17, cost: 31 id: 4, cost: 34 id: 5, cost: 38 id: 8, cost: 40 id: 0, cost: 62 id: 24, cost: 63
|
354
|
+
|
355
|
+
id: 23, cost: 65 id: 10, cost: 67 id: 13, cost: 67 id: 15, cost: 68 id: 11, cost: 68 id: 22, cost: 69 id: 7, cost: 71 id: 20, cost: 71 id: 19, cost: 73 id: 28, cost: 73 id: 14, cost: 79 id: 27, cost: 84 id: 6, cost: 87 id: 2, cost: 92 id: 21, cost: 93
|
356
|
+
|
357
|
+
```
|
358
|
+
|
359
|
+
でも、これは O(n^2・log(n)) の計算量なのでヒープソートとは言えません。
|