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)最大堆和最小堆的判断+构建树的更多相关文章

  1. PAT 1147 Heaps[难]

    1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...

  2. PAT 1147 Heaps

    https://pintia.cn/problem-sets/994805342720868352/problems/994805342821531648 In computer science, a ...

  3. C++ multiset通过greater、less指定排序方式,实现最大堆、最小堆功能

    STL中的set和multiset基于红黑树实现,默认排序为从小到大. 定义三个multiset实例,进行测试: multiset<int, greater<int>> gre ...

  4. Google 面试题:Java实现用最大堆和最小堆查找中位数 Find median with min heap and max heap in Java

    Google面试题 股市上一个股票的价格从开市开始是不停的变化的,需要开发一个系统,给定一个股票,它能实时显示从开市到当前时间的这个股票的价格的中位数(中值). SOLUTION 1: 1.维持两个h ...

  5. c++/java/python priority_que实现最大堆和最小堆

    #include<iostream>#include<vector>#include<math.h>#include<string>#include&l ...

  6. [PAT] 1147 Heaps(30 分)

    1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...

  7. STL 最大堆与最小堆

    在第一场CCCC选拔赛上,有一关于系统调度的水题.利用优先队列很容易AC. // 由于比赛时花费了不少时间研究如何定义priority_queue的比较函数,决心把STL熟练掌握... Queue 首 ...

  8. -Xmx 和 –Xms 设置最大堆和最小堆

    C:\Java\jre1.6.0\bin\javaw.exe 按照上面所说的,最后参数在eclipse.ini中可以写成这个样子: -vmargs     -Xms128M     -Xmx512M ...

  9. Python3实现最小堆建堆算法

    今天看Python CookBook中关于“求list中最大(最小)的N个元素”的内容,介绍了直接使用python的heapq模块的nlargest和nsmallest函数的解决方式,记得学习数据结构 ...

随机推荐

  1. 【noi 2.6_1808】最长公共子序列(DP)

    题意:给2个字符串求其最大公共子序列的长度.解法:这个和一般的状态定义有点不一样,f[i][j]表示 str 前i位和 str2 前j的最大公共子序列的长度,而不是选 str 的第i位和 str2 的 ...

  2. WSL ubuntu重置密码

    1. 在powershell中切换到root: 2. 进入ubuntu: 3. 修改制定用户的密码: 4. 切换回默认的用户:

  3. SpringBoot 启动慢的解决办法

    项目集成了很多内容,有 700 多个类,IDEA 中启动一次需要 70 秒,非常影响开发效率. 研究问题原因发现有以下几种情况会导致启动速度慢,优化后启动只需 26 秒左右了: 1. 和网卡有关,禁用 ...

  4. how to read the system information by using the node cli tool?

    how to read the system information by using the node cli tool? node cli & get system info demos ...

  5. iframe & sandbox & 微前端

    iframe & sandbox & 微前端 沙箱,容器,隔离 sandbox demo svg progress bar https://stackoverflow.com/ques ...

  6. Azure 计费 & 成本管理 & 取消 Azure 订阅

    Azure 计费 & 成本管理 & 取消 Azure 订阅 https://docs.microsoft.com/zh-cn/azure/cost-management-billing ...

  7. V8 & ECMAScript & ES-Next

    V8 & ECMAScript & ES-Next ES6, ES7, ES8, ES9, ES10, ES11, ES2015, ES2016, ES2017, ES2018, ES ...

  8. React & CSS Modules & CSS in JS

    React & CSS Modules & CSS in JS https://codesandbox.io/s/css-modules-name-mangling-ck1eo CSS ...

  9. 以太坊手续费上涨,矿工出逃,VAST前景向好!

    根据最新数据显示,以太坊的Gas费用在最近几天大幅飙涨,尤其是在过去2小时内,增幅约20%,一度达到了17.67美元.而这也导致了,许多基于以太坊协议的相关项目无法被生态建设者使用,很多矿工也纷纷出逃 ...

  10. 「NGK每日快讯」12.22日NGK第49期官方快讯!