1155 Heap Paths
题干前半略。
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<stdio.h>
#include<algorithm>
#include<vector> using namespace std; int seq[1005];
vector<int> result[1005];
vector<int> temp;
int num;
int c;
void dfs(int root)
{
if(root*2+1>num)
{
temp.push_back(seq[root]);
result[c]=temp;
c++;
temp.pop_back();
return ;
}
temp.push_back(seq[root]);
if(root*2+2<num) dfs(root*2+2);
if(root*2+1<num) dfs(root*2+1);
temp.pop_back();
return ;
}
int mark;
bool flag;
int main()
{ scanf("%d",&num);
for(int i=0;i<num;i++)
{
scanf("%d",&seq[i]);
}
dfs(0);
if(seq[0]<seq[1]) mark=-1; //minheap
else mark=1; //maxheap
for(int i=0;i<c;i++)
{
for(int t=0;t<result[i].size();t++)
{
if(t!=0)
{
if(mark==1)
{
if(result[i][t]>result[i][t-1]) flag=true;
}
else if(mark==-1)
{
if(result[i][t]<result[i][t-1]) flag=true;
}
}
printf("%d",result[i][t]);
if(t!=result[i].size()-1) printf(" ");
}
printf("\n");
}
if(flag==true)
{
printf("Not Heap");
}
else
{
if(mark==1) printf("Max Heap");
else printf("Min Heap");
} }
1155 Heap Paths的更多相关文章
- PTA 1155 Heap Paths (DFS)
题目链接:1155 Heap Paths (30 分) In computer science, a heap is a specialized tree-based data structure t ...
- PAT 甲级 1155 Heap Paths
https://pintia.cn/problem-sets/994805342720868352/problems/1071785408849047552 In computer science, ...
- pat甲级 1155 Heap Paths (30 分)
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
- PAT Advanced 1155 Heap Paths (30 分)
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
- PAT Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]
题目 In computer science, a heap is a specialized tree-based data structure that satisfies the heap pr ...
- 1155 Heap Paths (30 分)(堆+dfs遍历)
比较简单的一题 遍历左右的时候注意一下 #include<bits/stdc++.h> using namespace std; ; ]; ; vector<int>t; ve ...
- PAT甲级 1155 Heap Paths (30分) 堆模拟
题意分析: 给出一个1000以内的整数N,以及N个整数,并且这N个数是按照完全二叉树的层序遍历输出的序列,输出所有的整条的先序遍历的序列(根 右 左),以及判断整棵树是否是符合堆排序的规则(判断是大顶 ...
- PAT_A1155#Heap Paths
Source: PAT A1155 Heap Paths (30 分) Description: In computer science, a heap is a specialized tree-b ...
- PAT A1155 Heap Paths (30 分)——完全二叉树,层序遍历,特定dfs遍历
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
随机推荐
- 第2章 HTML中的JavaScript
目录 1. script标签 1.1 标签位置 1.2 defer推迟执行脚本 1.3 async异步执行脚本 1.4 动态加载脚本 2. noscript标签 1. script标签 <scr ...
- Java 中 Executors.newSingleThreadExecutor() 与Executors.newFixedThreadPool(1)有什么区别
在研究Executors提供的线程池时自然会想到标题这个问题,既然已经有了newFixedThreadPool,为什么还要存在newSingleThreadExecutor这个方法.难道newFixe ...
- Go语言从入门到放弃(设置 go get 为国内源)
前言 Go语言学到 Gin 框架了, 其实每天学习是比较辛苦的事情, 坚持下去! 在使用 Go 过程中发现, 最无奈的是Go的一些模块下不下来, 即便挂了V, 油管2k不卡的那种, 依旧是 time ...
- Mongodb 安装和副本集集群搭建
通用步骤,适用于所有你需要用的软件. 总结为5大步骤: 找到官网-->下载包-->解压-->修改配置-->启动 不懂的,首选官网api,次选百度 1.安装mongodb mon ...
- 彻底搞懂MySQL为什么要使用B+树索引
目录 MySQL的存储结构 表存储结构 B+树索引结构 B+树页节点结构 为什么要用B+树索引 二叉树 多叉树 B树 B+树 搞懂这个问题之前,我们首先来看一下,MySQL表的存储结构 MySQL的存 ...
- python—base64
今天在写题时,执行脚本又报错了 脚本如下 #! /usr/bin/env python3 # _*_ coding:utf-8 _*_ import base64 # 字典文件路径 dic_file_ ...
- 编年史:OI算法总结
目录(按字典序) A --A* D --DFS找环 J --基环树 S --数位动规 --树形动规 T --Tarjan(e-DCC) --Tarjan(LCA) --Tarjan(SCC) --Ta ...
- 特斯拉Toolbox诊断检测仪工具Tesla诊断电脑 Tesla Toolbox
Tesla特斯拉Toolbox诊断工具Tesla诊断电脑检测仪 Tesla Toolbox, Tesla Toolbox Diagnostic Tester.Language: English,Deu ...
- VMwareWorkstation 平台 Ubuntu14 下安装配置 伪分布式 hadoop
VMwareWorkstation平台Ubuntu14下安装配置伪分布式hadoop 安装VmwareStation 内含注册机. 链接:https://pan.baidu.com/s/1j-vKgD ...
- Fixing SQL Injection: ORM is not enough
Fixing SQL Injection: ORM is not enough | Snyk https://snyk.io/blog/sql-injection-orm-vulnerabilitie ...