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 2976】Dropping tests(算法效率--01分数规划 模版题+二分){附【转】01分数规划问题}
P.S.又是一个抽时间学了2个小时的新东西......讲解在上半部分,题解在下半部分. 先说一下转的原文:http://www.cnblogs.com/perseawe/archive/2012/05 ...
- zjnu1725 COCI (类似二维树状数组模拟)
Description The 3rd round of COCI is already here! In order to bet on predict the scores, we have as ...
- STL中pair容器的用法
1.定义pair容器 1 pair <int, int> p, p1; 2 //定义 [int,int] 型容器 //直接初始化了p的内容 pair<string,int>p( ...
- CF1400-D. Zigzags
CF1400-D. Zigzags 题意: 给出一个由\(n\)个数字构成的数组\(a\),让你在这个数组中找出有多少个符合以下要求的元组\((i,j,k,l)\): 1. \(i<j<k ...
- PTA L1-006 连续因子【暴力模拟】
一个正整数N的因子中可能存在若干连续的数字.例如630可以分解为3*5*6*7,其中5.6.7就是3个连续的数字.给定任一正整数N,要求编写程序求出最长连续因子的个数,并输出最小的连续因子序列. 输入 ...
- Linux 驱动框架---linux 驱动
总述 Linux 系统下的驱动最后都是以如下这个结构体呈现在系统中的,注意其中的dev_pm_ops是内核新增的内容来准备替换platform_driver中的电源管理相关的内容.这里内容是先进行总体 ...
- css sticky & 吸顶效果
css sticky & 吸顶效果 demo https://codepen.io/xgqfrms/pen/PoqyVYz css position sticky not working ht ...
- React Native & Fast Refresh
React Native & Fast Refresh 0.61 https://reactnative.dev/blog/2019/09/18/version-0.61/ Fast Refr ...
- javascript & call & apply & bind & new
javascript & call & apply & bind & new Javascript call() & apply() vs bind()? ht ...
- 从长度为 M 的无序数组中,找出N个最小的数
从长度为 M 的无序数组中,找出 N个最小的数 在一组长度为 n 的无序的数组中,取最小的 m个数(m < n), 要求时间复杂度 O(m * n) 网易有道面试题 const minTopK ...