PAT甲级 1155 Heap Paths (30 分)

PAT 1155 Heap Paths (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... 阅读详情

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) the key of C. A common implementation of a heap is the binary heap, in which the tree is a complete binary tree. (Quoted from Wikipedia at https://en.wikipedia.org/wiki/Heap_(data_structure))

One thing for sure is that all the keys along any path from the root to a leaf in a max/min heap must be in non-increasing/non-decreasing order.

Your job is to check every path in a given complete binary tree, in order to tell if it is a heap or not.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (1<N≤1,000), the number of keys in the tree. Then the next line contains N distinct integer keys (all in the range of int), which gives the level order traversal sequence of a complete binary tree.

Output Specification:

For each given tree, first print all the paths from the root to the leaves. Each path occupies a line, with all the numbers separated by a space, and no extra space at the beginning or the end of the line. The paths must be printed in the following order: for each node in the tree, all the paths in its right subtree must be printed before those in its left subtree.

Finally print in a line Max Heap if it is a max heap, or Min Heap for a min heap, or Not Heap if it is not a heap at all.

Sample Input 1:

8
98 72 86 60 65 12 23 50

Sample Output 1:

98 86 23
98 86 12
98 72 65
98 72 60 50
Max Heap

Sample Input 2:

8
8 38 25 58 52 82 70 60

Sample Output 2:

8 25 70
8 25 82
8 38 52
8 38 58 60
Min Heap

Sample Input 3:

8
10 28 15 12 34 9 8 56

Sample Output 3:

10 15 8
10 15 9
10 28 34
10 28 12 56
Not Heap
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;
int n;
int heap[1005];
int l;
vector<int> v;
int status = -1;// 初始化为-1,0为min,1为max,2为状态翻转

int judge(int a, int b) {
    if (a < b) return 0;
    else
        return 1;
}

void print_route(int m) {
    if (m * 2 > n && m * 2 + 1 > n) {
        if (m <= n) {
            for (int i = 1; i < v.size(); ++i) {
                if (status == -1)
                    status = judge(v[i - 1], v[i]);
                if (status != judge(v[i - 1], v[i]))
                    status = 2;
                if (i - 1 == 0)
                    cout << v[i - 1];
                else
                    cout << " " << v[i - 1];
            }
            cout << " " << v[v.size() - 1] << endl;
        }
    } else {
        v.push_back(heap[m * 2 + 1]);
        print_route(m * 2 + 1);
        v.pop_back();
        v.push_back(heap[m * 2]);
        print_route(m * 2);
        v.pop_back();
    }
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> heap[i];
    v.push_back(heap[1]);
    print_route(1);
    if (status == 2)
        cout << "Not Heap";
    else if (status == 1)
        cout << "Max Heap";
    else
        cout << "Min Heap";
    return 0;
}
18年冬季第四题 PAT甲级 1155 Heap Paths (30分) 题目 题目来源:https://pintia.cn/problem-sets/994805342720868352/problems/1071785408849047552 In computer science, aheapis a specialized tree-based data structure that satisfies the heap property: if P i... 阅读详情

相关推荐

基于Ultralytic实现多路摄像头目标检测

基于Ultralytic实现多路摄像头目标检测前面的文章分享了单路摄像头画面的YOLO目标检测,这篇文章主要分享一下多路摄像头画面的实时YOLO目标检测。测试环境在Windows系统下进行。

qq_57674776的博客 941

PAT|1155 Heap Paths(堆、递归)

题目大意 在计算机科学中,堆是一种特殊的基于树的数据结构,它满足堆属性:如果 P 是 C 的父节点,则 P 的键(值)要么大于或等于(在最大堆中) ) 或小于或等于(在最小堆中)C 的键。堆的常见实现是二叉堆,其中树是完全二叉树。 (引自维基百科 https://en.wikipedia.org/wiki/Heap_(data_structure)) 可以肯定的是,最大/最小堆中从根到叶的任何路径上的所有键都必须按非递增/非递减顺序排列。 你的工作是检查给定的完整二叉树中的每条路径,以确定它是否是一个堆。

qq_43528044的博客 233

数字后端实战指南 | Innovus Function ECO全流程解析(从LAB到Tapeout)

本文深入解析了在Cadence Innovus工具中进行Function ECO的全流程实战指南。从LAB环境搭建到Tapeout前的签核验证,详细拆解了Pre-mask与Post-mask ECO的核心步骤、关键命令(如ecoDesign)以及常见避坑方案。重点阐述了如何应对时序、DRC等挑战,并强调了备用单元管理与严格验证的重要性,是数字后端工程师提升综合能力的必备参考。

weixin_28452161的博客 641

PAT A1155 Heap Paths (30 分)

PAT A1155 Heap Paths (30 分)

zhengqinyuanbift的博客 248

Windows游戏玩家必看:用Process Lasso优化CPU优先级和核心绑定,告别卡顿

本文为Windows游戏玩家详细介绍了如何使用Process Lasso优化系统性能。通过设置CPU优先级和核心绑定,可以智能管理进程资源,确保游戏获得最高调度权限,有效减少因后台程序干扰或Windows调度机制不完善导致的卡顿与帧数波动,从而提升游戏流畅度与稳定性。

weixin_29299807的博客 2218

图数据挖掘 ECC K-core core number truss number clique

K-core:什么叫k-core,给定一个正整数k(k>=0),k-core是一个G的最大子图(也就是说k-core是一个图的限制),将这个子图定义为Hk,对于Hk中的每个顶点v,他的度数都是大于等于k的。 k-core是内嵌的,如下图所示,集合{ABCD}是H3,也就是3-core的子图。而集合{ABCDE}是H2,也就是2-core的子图,集合{ABCDEGF}是H1,也就是1-core的子图,而从集合的角度来讲H3<H2<H1。 core number:什么叫kcor

weixin_45193103的博客 1686

1147 Heaps

题目 题意:给定完全二叉树的层序遍历,问该二叉树是否为堆。为什么堆,以及输出它的后序遍历 #include<iostream> #include<vector> using namespace std; int n,m; int s[1003]; vector<int>post; bool maxheap() { for(int i=0; i<n...

qq_1300258993的博客 420

嘉立创EDA实战:从点亮第一盏灯到完成首块PCB

本文详细介绍了使用嘉立创EDA从点亮第一盏LED灯到完成首块PCB的全过程。通过实战案例分享,包括原理图绘制、PCB设计、DRC检查、覆铜操作等关键步骤,帮助电子设计爱好者快速掌握嘉立创EDA的使用技巧,并避免常见的设计错误。

weixin_30314793的博客 373

#1155. Heap Paths【完全二叉树 + 堆 + DFS】

原题链接 Problem Description: 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

geraltofrivia123的博客 200

图解转置卷积原理

声明:本文大部分搬运自【机器学习】详解 转置卷积 (Transpose Convolution) 感谢博主花与人间事同,本人只是在博主原创文章上做补充以及添加一些自己的理解。 1 转置卷积的背景 通常,对图像进行多次卷积运算后,特征图的尺寸会不断缩小。而对于某些特定任务 (如图像分割和图像生成等),需将图像恢复到原尺寸再操作。这个将图像由小分辨率映射到大分辨率的尺寸恢复操作,称为上采样 (Upsample),如下图所示: 上采样方法有很多,详见【图像处理】详解 最近邻插值、线性插值、双线性插值、双三次插值

laonafahaodange的博客 5686

[AHK]获取通达信软件上的股票代码

PC上的股票交易软件的自动止损、自动下单等功能比较弱,不如期货交易软件。 自力更生,程序员都是自己打造工具。 根据IPO模型(Input 输入,Process处理,Output输出),为了方便自动化首先要获取当前交易软件上浏览的股票代码。 截图再OCR显然太麻烦,通过Winspy工具查看下交易软件自带功能的消息号(33780),大家记住这个号码! 下面在用AutoHotkey作为变成语...

liuyukuan的专栏 1万+

1155 Heap Paths (30 分)

1155 Heap Paths (30 分) 题目大意 给定完全二叉树层序遍历得到的序列,输出每条叶子结点到根结点的路径(从右向左输出)。 基本思路 判断属于什么堆思路这里就不写了,请看A1147的思路。这里主要说明一下如何从右向左输出每一条路径: 定义一个数组depthc存放每个叶子结点的层次,index代表最高高度和第二高高度的临界叶子结点的编号,函数solve用来求路径(逆序),cnt存放当前是第几条路径,vector path[maxn]存放第cnt条路径的所有结点(逆序存放) 求出每个叶子节点的

Baifeili的博客 186

Python 爬虫教程:美食网站 + 社交平台餐馆评论抓取(评分提取 + 情感分析)

在当今数字化时代,数据无处不在,而爬虫技术则是获取数据的强大工具之一。无论是市场调研、学术研究还是个人兴趣探索,爬虫都能帮助我们从海量的网络信息中提取有价值的数据。本文将通过一个实战案例,带领大家学习如何使用 Python 爬虫技术抓取美食网站和社交平台上的餐馆评论,并进行评分提取与情感分析。通过这个项目,你将掌握爬虫的基本原理、数据解析、以及如何结合情感分析技术来挖掘数据背后的洞察。

u014481728的博客 311

PAT 甲级 1155 Heap Paths (30分)

1155 Heap Paths

Droke_Zhou的博客 362

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

PAT甲级1155 Heap Paths (30 分)

1155Heap Paths(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 gre...

Dexter 227

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

PAT 甲级 1155 Heap Paths 30分

PAT 甲级 1155 Heap Paths 30分题目解析代码 题目 原题链接: 题目描述 解析 判断一串数,是否构成大顶堆、小顶堆或不是堆 并按从右子树到左子树的深度优先遍历顺序输出所有遍历的情况 代码 #include<stdio.h> #include<vector> using namespace std; vector<int> heap; in...

weixin_41699413的博客 295

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

【PAT甲级题解记录】1155 Heap Paths (30 分)

问题描述 给一棵完全二叉树,求其是否为堆,要求输出大根堆还是小根堆,并且输出每一条自上而下的分支路径。 解题思路 对于路径的输出,利用dfs(其实就是二叉树的NRL遍历,和先序遍历差不多,先序遍历是NLR)配合动态维护vector就可以完成。 至于堆性质的判定,我们可以直接判断heap[0]和heap[1]的大小顺序,然后以此顺序为参照,在dfs里判断path是否按照该顺序排序。

retr0的博客 344

PAT-甲级-1155 Heap Paths (30 分)

1155Heap Paths(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 gre...

valcanoshan的博客 163

PAT 甲级 1155 Heap Paths (30 分)

题目 题目跳转 1155 Heap Paths (30 分) 分析 题目大意是判断给定的层级遍历的二叉树是不是堆,是的话是最大堆还是最小堆。这里看完了算法笔记上对堆的分析,有一个要点,就是可以用数组表示堆 用n*2表示左节点2n+1表示右节点。然后就根据定义写30分就到手了。 code #include <stdio.h> #include <iostream> #include <vector> using namespace std; const int M = 1

code_garden 209

PAT甲级 -- 1155 Heap Paths (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 to (i...

猪の成长日记 227

PAT 甲级 1155 Heap Paths (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 to...

u011381669的博客 160

PAT 1155 Heap Paths (30分)- 甲级

这是目前PAT的最后一题了,一开始做的时候,想到了dfs,但是由于只简单学了数据结构,第一次碰到这种类型的题目最开始的想法就是建立线索二叉树,后面又从二叉树性质入手(左右孩子和父节点的关系),利用栈的LIFO做,但是,没能成功实现,最后看了柳神的代码,豁然开朗,STL太好用了! #include<cstdio> #include<iostream> #include&lt...

Oyang的blog 217

PAT甲级A1155 Heap Paths (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...

vir_lee的博客 343
上一篇: PAT乙级 1095 解码PAT准考证 (25 分)
下一篇: PAT甲级 1147 Heaps (30 分)
XdpCs
博客等级 码龄6年 327粉丝 357原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XdpCs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值