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函数的解决方式,记得学习数据结构 ...
随机推荐
- P3803 [模板] 多项式乘法 (FFT)
Rt 注意len要为2的幂 #include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); inli ...
- 【uva 1152】4 Values Whose Sum is Zero(算法效率--中途相遇法+Hash或STL库)
题意:给定4个N元素几个A,B,C,D,要求分别从中选取一个元素a,b,c,d使得a+b+c+d=0.问有多少种选法.(N≤4000,D≤2^28) 解法:首先我们从最直接最暴力的方法开始思考:四重循 ...
- Bubble Cup 13 - Finals [Online Mirror, unrated, Div. 1] K. Lonely Numbers (数学)
题意:定义两个数\(a,b\)是朋友,如果:\(gcd(a,b)\),\(\frac{a}{gcd(a,b)}\),\(\frac{b}{gcd(a,b)}\)能构成三角形,现在给你一个正整数\(n\ ...
- 四、Jmeter 集合点(实际场景应用)
一.jmeter集合点的作用域及作用范围 先明确一些概念:1)定时器是在每个sampler(采样器)之前执行的,而不是之后: 是的,你没有看错,不管这个定时器的位置放在sampler之后,还是之下,它 ...
- 国产网络损伤仪 SandStorm -- 只需要拖拽就能删除链路规则
国产网络损伤仪SandStorm可以模拟出带宽限制.时延.时延抖动.丢包.乱序.重复报文.误码.拥塞等网络状况,在实验室条件下准确可靠地测试出网络应用在真实网络环境中的性能,以帮助应用程序在上线部署前 ...
- 爬虫入门二 beautifulsoup
title: 爬虫入门二 beautifulsoup date: 2020-03-12 14:43:00 categories: python tags: crawler 使用beautifulsou ...
- 【非原创】codeforces - 1067A Array Without Local Maximums【dp】
学习博客:戳这里 附本人代码: 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 co ...
- pyspark+anaconda配置
参考 https://www.e-learn.cn/content/python/786199 注意 所有的变量都放在环境变量而非用户变量.比如JAVA_HOME. 不然虽然pyspark没问题,但是 ...
- _.shuffle、_.debounce中下划线对象的理解
Vue 官方教程中有_.shuffle._.debounce,不明白"_"是怎么来的,有什么意义? Lodash 和 Underscorejs 都有相关解释
- 洛谷p1966 火柴排队 (逆序对变形,目标排序
题目描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之间的距离定义为: ∑(ai-bi)^2 其中 ai 表示 ...