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函数的解决方式,记得学习数据结构 ...
随机推荐
- zoj2112 Dynamic Rankings (主席树 || 树套树)
The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with t ...
- codeforces #345 (Div. 1) D. Zip-line (线段树+最长上升子序列)
Vasya has decided to build a zip-line on trees of a nearby forest. He wants the line to be as long a ...
- 【uva 658】It's not a Bug, it's a Feature!(图论--Dijkstra或spfa算法+二进制表示+类“隐式图搜索”)
题意:有N个潜在的bug和m个补丁,每个补丁用长为N的字符串表示.首先输入bug数目以及补丁数目.然后就是对M个补丁的描述,共有M行.每行首先是一个整数,表明打该补丁所需要的时间.然后是两个字符串,第 ...
- c++派生类中构造函数和析构函数执行顺序、判断对象类型、抽象类、虚函数
一. 代码: 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include&l ...
- VSCode配置Python开发环境
https://blog.csdn.net/vinkim/article/details/81546333 https://zhuanlan.zhihu.com/p/31417084
- MySQL 回表查询 & 索引覆盖优化
回表查询 先通过普通索引的值定位聚簇索引值,再通过聚簇索引的值定位行记录数据 建表示例 mysql> create table user( -> id int(10) auto_incre ...
- 数据库之ODPS中sql语句指南
此篇博文为本人在实际工作中应用总结,转载请注明出处. 持续更新中 一.增 1.增加一列(向csp_hsy_count_info表中增加sale_qty列) ALTER TABLE csp_hsy_co ...
- Redis 的缓存淘汰机制(Eviction)
本文从源码层面分析了 redis 的缓存淘汰机制,并在文章末尾描述使用 Java 实现的思路,以供参考. 相关配置 为了适配用作缓存的场景,redis 支持缓存淘汰(eviction)并提供相应的了配 ...
- woj1013 Barcelet 字符串 woj1014 Doraemon's Flashlight 几何
title: woj1013 Barcelet 字符串 date: 2020-03-18 18:00:00 categories: acm tags: [acm,字符串,woj] 字符串,字典序. 1 ...
- mysql 索引类型以及创建
明天就去面浦发了,感觉对数据库有些忘了,时间紧迫,就直接把链接贴这了,有空再整理. 参考: 1. https://www.cnblogs.com/crazylqy/p/7615388.html