爆刷PAT(甲级)——之【1147】 Heaps(30 分)——堆的判别

18年春季第四题 PAT甲级 1147 Heaps (30分) 可以不建树 题目 来源:https://pintia.cn/problem-sets/994805342720868352/problems/994805342821531648 In computer science, aheapis a specialized tree-based data structure that satisfies the heap property: if P is a... 阅读详情

艰难的英语单词:postorder traversal   后序遍历

题意:给一个完全二叉树,判断是最大堆还是最小堆还是都不是。

难点:完全二叉树用数组存的时候(个人习惯下标从1开始),左孩子节点是父亲节点*2,右孩子是父亲节点*2+1。对于一个玩完全二叉树来说,N个节点,那么有N/2个叶子!

知道上面知识点以后,本题就很简单,从节点1到N/2,判断是否都符合最大堆或者最小堆特性,否则就什么都不是。

Code:

#include<bits/stdc++.h>
using namespace std;
#define inf 1009
#define INF 0x3f3f3f3f
#define loop(x,y,z) for(x=y;x<z;x++)

int n,m;
int e[inf];
bool tag;//第一个输出标记

bool isMaxHeap()
{
    int i;
    loop(i,1,n/2+1)
    {
        if(e[i]<e[i*2])return false;
        if(i*2+1<=n&&e[i]<e[i*2+1])return false;
    }
    return true;
}

bool isMinHeap()
{
    int i;
    loop(i,1,n/2+1)
    {
        if(e[i]>e[i*2])return false;
        if(i*2+1<=n&&e[i]>e[i*2+1])return false;
    }
    return true;
}

void postOrder(int v)
{
    if(v*2<=n)postOrder(v*2);
    if(v*2+1<=n)postOrder(v*2+1);
    if(tag)
    {
        printf("%d",e[v]);
        tag=false;
    }
    else
        printf(" %d",e[v]);
}

void Input()
{
    scanf("%d%d",&m,&n);
    while(m--)
    {
        int i;
        tag=true;
        loop(i,1,n+1)
            scanf("%d",&e[i]);

        if(isMaxHeap())
            printf("Max Heap\n");
        else if(isMinHeap())
            printf("Min Heap\n");
        else
            printf("Not Heap\n");
        postOrder(1);
        printf("\n");
    }
}

int main()
{
    Input();
    return 0;
}

 

Sigrity IBIS建模实战:如何用T2B工具快速转换SPICE模型(附避坑指南) 本文详细介绍了如何使用Sigrity T2B工具将SPICE模型高效转换为IBIS模型,涵盖从环境配置、模型预处理到转换流程和验证策略的全过程。通过实战案例和避坑指南,帮助硬件工程师掌握高速电路设计中的IBIS建模技术,提升信号完整性分析效率。 阅读详情

相关推荐

Amesim与Simulink联合仿真:S-Function接口配置与实战避坑指南

本文详细介绍了Amesim与Simulink联合仿真中S-Function接口的配置方法与实战避坑指南。从环境搭建、编译器配置到S-Function接口实战,逐步解析关键步骤与常见问题解决方案,帮助工程师高效实现多领域系统仿真,特别适合汽车、航空航天等复杂系统建模场景。

weixin_29173777的博客 344

PAT甲级 1147 Heaps (30 分)

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) t

DoMoreSpeakLess的博客 182

告别第三方工具!用Windows自带蓝牙玩转BLE设备调试(保姆级图文教程)

本文详细介绍了如何利用Windows自带的蓝牙功能进行BLE设备调试,无需第三方工具。从硬件准备、系统配置到实战操作,提供保姆级图文教程,帮助开发者高效完成设备发现、数据交互等全流程调试工作。特别适合物联网开发者和智能硬件爱好者。

o5p6q的博客 726

PAT 1147 Heaps(30 分)

In computer science, aheapis a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal t...

weixin_30516243的博客 262

手把手教你在Keil/IAR中移植SEGGER_RTT组件(避坑指南+性能对比)

本文详细介绍了在Keil/IAR开发环境中移植SEGGER RTT调试组件的完整流程与优化技巧。通过对比传统串口调试,SEGGER RTT凭借零硬件占用、高速传输和双向交互等优势,显著提升嵌入式开发效率。文章包含配置步骤、性能调优方案及常见问题解决方案,是嵌入式开发者必备的实用指南。

y7z8a的博客 243

FIFO Generate IP核使用——FIFO写操作详解及Status Flags页配置

本文介绍了FIFO的写操作及Status Flags页的配置信息。

u011565038的博客 2284

PTA 1147 Heaps(30 分)

题意:判断是最大堆还是最小堆或者不是堆 思路:直接分析i i*2 i*2+1之间的关系 注意点:输入的数字是在int范围内的所有数字,一开始的时候,我把数组置空成-1的然后错了好几个样例,回去看看题目,然后加了一个数组判断这个点是不是节点就可以了 #include&lt;bits/stdc++.h&gt; using namespace std; int tree[10050]; int i...

deep love deep do 406

SAC安装与相关库文件(.so)的安装

摘要 SAC: Seismic Analysis Code 是用于地震信号处理的一套软件。 本文将介绍SAC的安装,以及安装时遇到的一些问题。主要是一些动态库(lib**.so)找不到的问题。 本文的环境 64位的 Ubuntu 16.04 64位的系统是安装出现问题的主要原因 1. SAC获取与安装 SAC 的安装,只需要解压,然后修改环境变量即可!!!不需要任何编译!!! 1.1. SAC 获取与解压 SAC软件获取地址:http://ds.iris.edu/ds/nodes/dmc/forms/s.

WangCY的博客 4335

PAT甲级A1147 Heaps(30 分)

1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater t...

vir_lee的博客 330

避坑指南:Allegro 17.2更新封装时遇到的5个典型问题及解决方案(含Symbol报错处理)

本文深度解析Allegro 17.2封装更新过程中的核心机制与常见陷阱。针对“Update Symbols”功能失效、焊盘不匹配、DRC报错、图形层叠错误等5大典型问题,提供从原理分析到实战解决方案的完整指南,帮助用户实现从“更新失败”到“丝滑替换”的平稳过渡。

tgb34567890的博客 1039

华为HCIE之TS部分整理

TroubleShooting 整体拓扑 1. MSTP 题目需求:site1 中,CLIENT1 属于 VLAN12,CLIENT3 属于 VLAN34;MSTP 中的 VLAN12 属于 instance1,vlan34 属于 instance2;两个 instance 的主备根桥分别在 SW1 和 SW2 上,并且要求 CLIENT1访问 R1 时经过的路径是 SW3-SW1-R1;同时要求 CLIENT3 访问 R1 时经过的路径是SW3-SW2-R1; 排错思路: ①查看SW3 SW1 SW

ledrsnet的博客 5756

PAT 甲级 1147 Heaps (30 分)

这题太太太坑爹了,不建树感觉不出来,写的很轻松,建了树就会发现题目是真的坑; 题目里明确讲是N distinct integer,但是在设计测试用例的时候,最后一个测试点在同一颗树里其实是有相同的数的; 感觉自己被出题的玩了@_@ 思路: 1.思路很简单,每一树的序列,第i个和第2i+1、2i+2比较大小(如果这两个存在的话); 2.递归后序遍历二叉树; 代码: #include<iostr...

Yuhan的博客 375

【路径规划】Theta_star算法机器人栅格地图最短路径规划【含Matlab源码 2618期】

Theta_star算法机器人栅格地图最短路径规划 完整的代码,方可运行;可提供运行操作视频!适合小白!

订阅付费专栏Matlab(奶茶价版),可赠送奶茶价版付费专栏指定代码1份; 364

PAT A1147 Heaps (30分) (堆的判定)

PAT甲级:A1147 Heaps (30分) In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either gre...

只想用最简短的代码刷题 238

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

【PAT甲级 - C++题解】1147 Heaps

📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!

Newin2020的博客 419

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

PAT甲级 1147 Heaps

PAT甲级 1147 Heaps PAT1147链接 题目大意 给定查询次数M和完全二叉树的结点个数N及每棵树的层序遍历,每次查询输出它的后序遍历,并判断是否为最大最小堆 # include<iostream> # include<cstring> # include<vector> using namespace std; const int maxn = 1010; vector<int>post; int layer[maxn]; //int Compl

qq_41805174的博客 171

PAT甲级 1147 Heaps (30分) 后序遍历+堆的判断/C++

1147 Heaps (30分) 题目大意:给定一个完全二叉树的层序遍历,要求判断其是否为堆,并输出后序遍历结果。 判断是否为堆,先确定为最大堆还是最小堆(比较开始的两个元素),再比较各个元素与其左右孩子的大小关系。 柳婼大神只用了25行代码解决!(传送门,学到了,还是要多看看大神的题解哒~) #include<iostream> //输入输出流头文件 #include<vector> //变长数组容器 usin

qq_43955154的博客 268

PAT 1147. Heaps (30) - 甲级

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (i...

柳婼 の blog 2430

PAT甲级1147 Heaps (30 分)

1147Heaps(30分) In computer science, aheapis a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater ...

Dexter 241

【PAT甲级A1147】Heaps (30分)(c++)

1147 Heaps (30分) 作者:CHEN, Yue 单位:浙江大学 代码长度限制:16 KB 时间限制:400 ms 内存限制:64 MB In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater t

小庄同学的博客 241

pat 甲级 堆 A1147 Heaps (30分)

1147 Heaps (30分) In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to

weixin_42358356的博客 184

PAT甲级刷题记录——1147 Heaps (30分)

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) t

一只奋斗中的本科生 252

PAT——1147 Heaps 甲级

1147 Heaps题目题意题解AC代码参考 题目 https://pintia.cn/problem-sets/994805342720868352/problems/994805342821531648 题意 判断给定的某棵树的中序序列是大顶堆、小顶堆或者不是顶堆,并给出该树的后序序列(左右根) 题解 根据中序序列中,下标index对应的左节点为index * 2,右节点为index * 2+1的特性判断顶堆,同时运用递归按照左右根的顺序输出即可 AC代码 #include<bits/stdc++

ljhsq的博客 159

PAT 甲级 1147  Heaps

1147Heaps(30 point(s)) In computer science, aheapis a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either g...

漫浸天空的雨色的备忘录~ 672

【题解】【PAT甲】1147 Heaps (30 分)(堆)

题目链接 题目描述 题目大意 解题思路 题解

Elephant_King的博客 638
上一篇: 爆刷PAT(甲级)——之【1029】 Median(25 分)——队列问题+内存优化
下一篇: 爆刷PAT(甲级)——之【1146】 Topological Order(25 分)——拓扑排序
仰天倀笑
博客等级 码龄10年 313粉丝 124原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值