PAT-1147(Heaps)最大堆和最小堆的判断+构建树
Heaps
PAT-1147
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<sstream>
#include<set>
#include<map>
#include<cmath>
#include<vector>
#include<unordered_map>
using namespace std;
int m,n;
const int maxn=1003;
int tree[maxn];
vector<int>ve;
void afterOrder(int num){
if(num>n){
return;
}
afterOrder(num<<1);
afterOrder(num<<1|1);
ve.push_back(tree[num]);
}
int main(){
cin>>m>>n;
while(m--){
ve.clear();
for(int i=1;i<=n;i++){
cin>>tree[i];
}
int flag=0;
for(int i=1;i<=n;i++){
int lnode=i<<1;
int rnode=i<<1|1;
if(lnode>n)
lnode=i;
if(rnode>n)
rnode=i;
if(tree[i]>=tree[lnode]&&tree[i]>=tree[rnode]){
if(tree[i]>tree[lnode]||tree[i]>tree[rnode]){
if(flag==1){//原来是小根堆
flag=-1;
}else{
if(flag==0)
flag=2;
}
}
}else if(tree[i]<=tree[lnode]&&tree[i]<=tree[rnode]){
if(tree[i]<tree[lnode]||tree[i]<tree[rnode]){
if(flag==2)
flag=-1;
else{
if(flag==0)
flag=1;
}
}
}else{
flag=-1;
}
// cout<<flag<<endl;
}
afterOrder(1);
if(flag==-1){
cout<<"Not Heap"<<endl;
}else if(flag==1){
cout<<"Min Heap"<<endl;
}else{
cout<<"Max Heap"<<endl;
}
for(int i=0;i<ve.size();i++){
if(i==(int)ve.size()-1){
cout<<ve[i]<<endl;
}else cout<<ve[i]<<" ";
}
}
return 0;
}
另一种更好的判断最大堆最小堆的方法:
#include<iostream>
#include<vector>
using namespace std;
const int maxn=1005;
int n,m;
vector<int> v(maxn);
void dfs(int i){
if(i>m)return;
dfs(2*i);
dfs(2*i+1);
printf("%d%s",v[i],i==1?"\n":" ");
}
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
int minheap=1,maxheap=1;
for(int j=1;j<=m;j++){
scanf("%d",&v[j]);
if(j>1 && v[j/2]>v[j])minheap=0;
if(j>1 && v[j/2]<v[j])maxheap=0;
}
if(minheap==1)printf("Min Heap\n");
else printf("%s\n",maxheap==1?"Max Heap":"Not Heap");
dfs(1);
}
return 0;
}
PAT-1147(Heaps)最大堆和最小堆的判断+构建树的更多相关文章
- PAT 1147 Heaps[难]
1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...
- PAT 1147 Heaps
https://pintia.cn/problem-sets/994805342720868352/problems/994805342821531648 In computer science, a ...
- C++ multiset通过greater、less指定排序方式,实现最大堆、最小堆功能
STL中的set和multiset基于红黑树实现,默认排序为从小到大. 定义三个multiset实例,进行测试: multiset<int, greater<int>> gre ...
- Google 面试题:Java实现用最大堆和最小堆查找中位数 Find median with min heap and max heap in Java
Google面试题 股市上一个股票的价格从开市开始是不停的变化的,需要开发一个系统,给定一个股票,它能实时显示从开市到当前时间的这个股票的价格的中位数(中值). SOLUTION 1: 1.维持两个h ...
- c++/java/python priority_que实现最大堆和最小堆
#include<iostream>#include<vector>#include<math.h>#include<string>#include&l ...
- [PAT] 1147 Heaps(30 分)
1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...
- STL 最大堆与最小堆
在第一场CCCC选拔赛上,有一关于系统调度的水题.利用优先队列很容易AC. // 由于比赛时花费了不少时间研究如何定义priority_queue的比较函数,决心把STL熟练掌握... Queue 首 ...
- -Xmx 和 –Xms 设置最大堆和最小堆
C:\Java\jre1.6.0\bin\javaw.exe 按照上面所说的,最后参数在eclipse.ini中可以写成这个样子: -vmargs -Xms128M -Xmx512M ...
- Python3实现最小堆建堆算法
今天看Python CookBook中关于“求list中最大(最小)的N个元素”的内容,介绍了直接使用python的heapq模块的nlargest和nsmallest函数的解决方式,记得学习数据结构 ...
随机推荐
- 【poj 3090】Visible Lattice Points(数论--欧拉函数 找规律求前缀和)
题意:问从(0,0)到(x,y)(0≤x, y≤N)的线段没有与其他整数点相交的点数. 解法:只有 gcd(x,y)=1 时才满足条件,问 N 以前所有的合法点的和,就发现和上一题-- [poj 24 ...
- Color Changing Sofa Gym - 101962B、Renan and Cirque du Soleil Gym - 101962C、Hat-Xor Gym - 101962E 、Rei do Cangaço Gym - 101962K 、Sorting Machine Gym - 101962M
Color Changing Sofa Gym - 101962B 题意:给你一个由字母构成的字符串a,再给你一个由0.1构成的字符串b.你需要在a字符串中找到一个可以放下b的位置,要保证b字符串中0 ...
- 牛客练习赛70 B.拼凑 (序列自动机)
题意:有一个模板串,有\(T\)个字符串,从字符串中找到某个子串,使得这个子串中的子序列包含模板串,求最短的子串的长度. 题解:找子序列,很容易想到序列自动机,根据序列自动机的原理,我们一定可以确保除 ...
- Codeforces Round #649 (Div. 2) A. XXXXX (贪心)
题意:有一个长度为\(n\)的数组,找一段最长子数组,使得其元素和为\(x\),如果存在,输出子数组的长度,否则输出\(-1\). 题解:这题我们要从元素和\(sum\)来考虑,首先,如果原数组的所有 ...
- Filebeat 日志收集
Filebeat 介绍 Filebeat 安装 # 上传代码包 [root@redis03 ~]# rz filebeat-6.6.0-x86_64.rpm # 安装 [root@redis03 ~] ...
- Linux网络文件下载
wget 以网络下载 maven 包为例 wget -c http://mirrors.shu.edu.cn/apache/maven/maven-3/3.5.4/binaries/apache-ma ...
- kubernetes实战-配置中心(三)配置服务使用apollo配置中心
使用配置中心,需要开发对代码进行调整,将一些配置,通过变量的形式配置到apollo中,服务通过配置中心来获取具体的配置 在配置中心修改新增如下配置: 项目信息: 配置: 重新打包镜像,使用apollo ...
- jira 优先级过滤
jira 优先级过滤 priority project = DAP AND issuetype = 故障 AND priority = high AND resolution = Unresolved ...
- CDN 工作原理剖析
CDN 工作原理剖析 CDN / Content Delivery Network / 内容分发网络 https://www.cloudflare.com/zh-cn/learning/cdn/wha ...
- 2020 Web 全栈面经
2020 Web 全栈面经 1.简历 2. 技术 3. 项目 4. 架构 5. 沟通,协作 6.成长 7. 面试技巧 准备 电话确认,面试流程,五险一金缴纳情况 有无笔试,几轮,面试时间,答复时间 细 ...