当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
2019.10.6训练总结
2019-11-10训练总结
The stones game
Setinterval, setTimeout and requestanimationframe
Memory layout of JVM objects
基辅周边的凄美废墟——切尔诺贝利的安全前往指南!
2019.11.20训练总结
函数指针、函数指针数组、计算器+转移表等归纳总结
Codeforces Round #652 (Div. 2)
JVM之虚拟机栈之动态链接
Database common interview questions (with answers)
CodeForces - 1151B 思维
Codeforces Round #652 (Div. 2)
2019.10.20 training summary
2019.10.16训练总结
在Activity外使用startActivity()方法报错原因与解决办法
同花顺炒股软件可靠吗,安全吗?
六度空间 bfs
Symphony tutorial
If I were in Beijing, where would it be better to open an account? In addition, is it safe to open an account online now?









