题干:https://pintia.cn/problem-sets/994805342720868352/problems/1071785408849047552
题解:50分钟,满分解答,对看我博客的各位我真的很抱歉,虽然我写了经验分享和心路历程这样的语句,但是现在冲刺的时候真的很难再分心出来去总结这些,虽然我自己在现场做的时候有感受。。。我更想把这些时间用来自己休息一下或者去做别的题目。。。。真的很抱歉
// A1155.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <bits/stdc++.h>
using namespace std;
struct node
{
int val;
node* left;
node* right;
};
int n;
vector<int> an(10010);
vector<vector<int>> path;
vector<int> temp_path;
node* bulid(int root) {
if (root > n) {
return NULL;
}
node* temp = new node();
temp->val = an[root];
temp->left = bulid(2 * root);
temp->right = bulid(2 * root+1);
return temp;
}
void dfs(node* root) {
if (root->right == NULL && root->left == NULL) {
path.push_back(temp_path);//叶子节点,左右都没有子树
return;
}
if (root->right != NULL) {
temp_path.push_back(root->right->val);
dfs(root->right);
temp_path.pop_back();
}
if (root->left != NULL) {
temp_path.push_back(root->left->val);
dfs(root->left);
temp_path.pop_back();
}
}
void dfs(int root) {
if (2* root >n && 2* root +1>n) {
path.push_back(temp_path);//叶子节点,左右都没有子树
return;
}
if (2 * root + 1 <= n) {
temp_path.push_back(an[2 * root + 1]);
dfs(2 * root + 1);
temp_path.pop_back();
}
if (2 * root <=n) {
temp_path.push_back(an[2 * root]);
dfs(2 * root);
temp_path.pop_back();
}
}
int main()
{
#ifndef ONLINE_JUDGE
FILE* S;
freopen_s(&S, "in.txt", "r", stdin);
#endif // !ONLINE_JUDGE
int t;
cin >> n;
for (int i = 1; i <=n; i++) {
cin >> an[i];
}
//node* root = bulid(1);
temp_path.push_back(an[1]);
dfs(1);
bool max_heap = true, min_heap = true;
for (int i = 0; i < path.size(); i++) {
cout << path[i][0];
for (int j = 1; j < path[i].size(); j++) {
cout << " " << path[i][j];
if (path[i][j] >= path[i][j - 1]) max_heap = false;
if (path[i][j] <= path[i][j - 1]) min_heap = false;
}
cout << endl;
}
if (max_heap) {
cout << "Max Heap\n";
}
else if (min_heap) {
cout << "Min Heap\n";
}
else {
cout << "Not Heap\n";
}
return 0;
}