当前位置:网站首页>1147 heaps (30 points)
1147 heaps (30 points)
2022-06-29 10:13:00 【Momo 623】
Answer key
Feel this 30 Minute question , It is relatively simple
Carelessness : Determine the type of heap : Big pile top Small cap pile It's not a pile
Anyway All output his postorder traversal sequence
When judging, you should pay attention to
Better from N --> 2 To judge , In this way, the judgment of boundary conditions can be subtracted
If h[i] > h[i/2] It's a small pile
If h[i] < h[i/2] It's a big pile
If all is satisfied , Description is not a heap
Code
#include <iostream>
#include <algorithm>
using namespace std;
const int n = 1e4 + 10;
int h[n];
int N, T;
int f = 0;
void dfs(int k)
{
if (k > N)
return;
dfs(k * 2);
dfs(k * 2 + 1);
if (f++ != 0)
cout << " ";
cout << h[k];
}
int main()
{
cin >> T >> N;
while (T--)
{
f = 0;
for (int i = 1; i <= N; i++)
scanf("%d", &h[i]);
int maxt = 0, mint = 0;
// When judging the heap It's best to look at it from big to small
// In this way, it is not necessary to judge the boundary conditions
for (int i = N; i >= 2; i--)
{
if (h[i] > h[i / 2])
mint = 1;
else
maxt = 1;
}
if (mint == maxt)
cout << "Not Heap\n";
else if (mint)
cout << "Min Heap\n";
else
cout << "Max Heap\n";
dfs(1);
cout << endl;
}
return 0;
}
边栏推荐
猜你喜欢
随机推荐
Pipeline details of IPC (interprocess communication)
点在多边形内外的判断
Listview of the basic component of the shutter
Summary of PHP memory horse technology research and killing methods
1098 Insertion or Heap Sort (25 分)
Shanke's C language 2018 exercise (Telecom)
Container of the basic component of the flutter
Leetcode MySQL database topic 180
2019icpc上海区域赛赛后总结
Rikka with Cake(线段树+线段树)
The collapsing "2.3 * 10 = 22" produced by multiplying float and int
[51nod 1215] array width
L1-009 sum of N numbers (20 points)
另类实现 ScrollView 下拉头部放大
2019.10.16训练总结
FreeRTOS (IX) - queue
Sixteen system counter and flow lamp
L2-3 这是二叉搜索树吗?-题解超精彩哦
LiferayPortal JSONWS反序列化漏洞(CVE-2020-7961)分析
时变和非时变









