2977,3110 二叉堆练习1,3——codevs
已知一个二叉树,判断它是否为二叉堆(小根堆)
二叉树的节点数N和N个节点(按层输入)
YES或NO
样例输入1
3
1 4 9
样例输入2
3
6 4 9
样例输出1
YES
样例输出2
NO
对于20%的数据 N≤20
对于50%的数据 N≤1000
对于100%的数据 N≤50000,每个节点≤10000
根据小根堆的性质:父节点的左右子节点的值小于父节点的值
奉上AC代码:
#include<bits/stdc++.h>using namespace std;
int n,a[1000000];
bool tp;
int main()
{cin>>n;memset(a,0x7f,sizeof(a));for(int i=1;i<=n;i++) cin>>a[i];for(int i=1;i<=n;i++){int l=i*2,r=i*2+1;if(a[l]<a[i]){tp=1;break;}else if(a[r]<a[i]){tp=1;break;}}if(tp==1) cout<<"NO";else cout<<"YES";
}
二叉堆练习3
给定N(N≤500,000)和N个整数(较有序),将其排序后输出。
N和N个整数
N个整数(升序)
5
12 11 10 8 9
8 9 10 11 12
对于33%的数据 N≤10000
对于另外33%的数据 N≤100,000 0≤每个数≤1000
对于100%的数据 N≤500,000 0≤每个数≤2*10^9
堆排序模板题:
奉上AC代码:
1.堆排序1
#include<bits/stdc++.h>using namespace std; int q,heap[500005]; void heapify(int num[],int i,int size){int left_child=2*i;int right_child=2*i+1;int maxn=i;if(left_child<size&&num[left_child]>num[maxn])maxn=left_child;if(right_child<size&&num[right_child]>num[maxn])maxn=right_child;if(maxn!=i){swap(num[i],num[maxn]);heapify(num,maxn,size);} } int buildheap(int num[],int n){int heap_size=n;for(int i=heap_size/2;i;i--)heapify(num,i,heap_size);return heap_size; } void heapsort(int num[],int n){int heap_size=buildheap(num,n);while(heap_size>1){swap(num[1],num[--heap_size]);heapify(num,1,heap_size);} } int main() {cin>>q;for(int i=1;i<=q;i++) cin>>heap[i];heapsort(heap,q+1);for(int i=1;i<=q;i++) cout<<heap[i]<<" ";return 0; }
View Code
2.堆排序2
根据插入以及弄出两种操作
#include<iostream>using namespace std; int heap[500010],heap_size; void put(int d) {int now, next;heap[++heap_size] = d;now = heap_size;while(now > 1){next = now >> 1;if(heap[now] >= heap[next]) break;swap(heap[now], heap[next]);now = next;} } int get() {int now=1, next, res= heap[1];heap[1] = heap[heap_size--];while(now * 2 <= heap_size){next = now * 2;if (next < heap_size && heap[next + 1] < heap[next]) next++;if (heap[now] <= heap[next]) break;swap(heap[now], heap[next]);now = next;}return res; }int main() {int n,x;cin>>n;for(int i=1;i<=n;i++){cin>>x;put(x);}for(int i=1;i<=n;i++){cout<<get()<<" ";}return 0; }
View Code
3.当然,也可以使用STL水过去
#include<bits/stdc++.h>using namespace std;
int n;
priority_queue<int ,vector<int>,greater<int> >heap;
int main()
{cin>>n;for(int i=1;i<=n;i++){int x;cin>>x;heap.push(x);}for(int i=1;i<=n;i++){cout<<heap.top()<<" ";heap.pop();}return 0;
}
转载于:https://www.cnblogs.com/song-/p/8717319.html
2977,3110 二叉堆练习1,3——codevs相关推荐
- 2977 二叉堆练习1
2977 二叉堆练习1 时间限制: 10 s 空间限制: 32000 KB 题目等级 : 白银 Silver 题目描述 Description 已知一个二叉树,判断它是否为二叉堆(小根堆) 输入描述 ...
- 2977 二叉堆练习1 codevs
题目描述 Description 已知一个二叉树,判断它是否为二叉堆(小根堆) 输入描述 Input Description 二叉树的节点数N和N个节点(按层输入) 输出描述 Output Descr ...
- codevs 2977 二叉堆练习1x
时间限制: 10 s 空间限制: 32000 KB 题目等级 : 白银 Silver 题目描述 Description 已知一个二叉树,判断它是否为二叉堆(小根堆) 输入描述 Input Descri ...
- 大根堆的删除c语言,二叉堆(一)之 C语言详解
本文介绍二叉堆,二叉堆就是通常我们所说的数据结构"堆"中的一种.和以往一样,本文会先对二叉堆的理论知识进行简单介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现: ...
- 在A*寻路中使用二叉堆
在A*寻路中使用二叉堆 作者:Patrick Lester(2003年4月11日更新) 译者:Panic 2005年3月28日 译者序: 这一篇文章,是"A* Pathfinding ...
- 0x17.基础数据结构 - 二叉堆
目录 一.二叉堆 二.例题 0.AcWing 145. 超市 AcWing 146. 序列(POJ 2442) 三.HuffmanHuffmanHuffman树 1.AcWing 148. 合并果子 ...
- 大顶堆删除最大值_算法学习笔记(47): 二叉堆
堆(Heap)是一类数据结构,它们拥有树状结构,且能够保证父节点比子节点大(或小).当根节点保存堆中最大值时,称为大根堆:反之,则称为小根堆. 二叉堆(Binary Heap)是最简单.常用的堆,是一 ...
- 排序算法之——优先队列经典实现(基于二叉堆)
许多应用都需要处理有序的元素,但有时,我们不要求所有元素都有序,或是一定要一次就将它们排序,许多情况下,我们会收集这些元素里的最大值或最小值. 这种情况下一个合适的数据结构应该支持两种操作:插入元素. ...
- 线段树、二叉堆以及离散化入门
目录 线段树 例题 题面 练习 1 2 3 4 5 小解区间操作 二叉堆 例题 思路 @ 线段树 例题 题面 时间限制: 1 Sec 内存限制: 128 MB [题意]给出N个数,两种操作:1.C x ...
最新文章
- java运算符-逻辑、三元运算符
- 阿里云智能对话分析服务
- JS中点语法和方括号语法访问属性的区别
- html5元素拖动 (转载黑桐)
- 浏览器的垃圾回收机制
- 怎样快速识别 英文地址中包含非英文字符_[论文笔记]端到端的场景文本识别算法--CRNN 论文笔记...
- aqs java 简书,Java AQS源码解读
- 开启一个新的命令行窗口
- 计算机网络学习笔记(一)——分层模型、协议、服务、连接模式、标准化组织
- 飞鸽传书2011绿色版简单性
- [转]C++编译链接过程详解
- Linkerd实战(2)示例详解
- 【UVM基础】虚序列器与虚序列(virtual sequencer与virtual sequence)快速上手指南
- 背包九讲之一:01背包问题
- 关于AMD64和arm64、x86和x86_64的介绍
- “校园舆情监测系统”网站设计及网页编辑总结
- 驳quot;世上流传最广的32条谣言quot;
- C++——文件指针定位--fseek,ftell
- RISC-V_GD32VF103-对 Debug 设置
- 用Matlab进行十进制和二进制数的转换