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";
}
给定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;
];
void heapify(int num[],int i,int size){
*i;
*i+;
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;
;i;i--)
heapify(num,i,heap_size);
return heap_size;
}
void heapsort(int num[],int n){
int heap_size=buildheap(num,n);
){
swap(num[],num[--heap_size]);
heapify(num,,heap_size);
}
}
int main()
{
cin>>q;
;i<=q;i++) cin>>heap[i];
heapsort(heap,q+);
;i<=q;i++) cout<<heap[i]<<" ";
;
}
2.堆排序2
根据插入以及弄出两种操作
#include<iostream>
using namespace std;
],heap_size;
void put(int d)
{
int now, next;
heap[++heap_size] = d;
now = heap_size;
)
{
next = now >> ;
if(heap[now] >= heap[next]) break;
swap(heap[now], heap[next]);
now = next;
}
}
int get()
{
, next, res= heap[];
heap[] = heap[heap_size--];
<= heap_size)
{
next = now * ;
] < 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;
;i<=n;i++){
cin>>x;
put(x);
};i<=n;i++){
cout<<get()<<" ";
}
;
}
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;
}
2977,3110 二叉堆练习1,3——codevs的更多相关文章
- codevs 3110 二叉堆练习3
3110 二叉堆练习3 http://codevs.cn/problem/3110/ 题目描述 Description 给定N(N≤500,000)和N个整数(较有序),将其排序后输出. 输入描述 I ...
- AC日记——二叉堆练习3 codevs 3110
3110 二叉堆练习3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 给定N(N≤500,000)和N个整 ...
- 2977 二叉堆练习1 codevs
题目描述 Description 已知一个二叉树,判断它是否为二叉堆(小根堆) 输入描述 Input Description 二叉树的节点数N和N个节点(按层输入) 输出描述 Output Descr ...
- codevs 2977 二叉堆练习1x
时间限制: 10 s 空间限制: 32000 KB 题目等级 : 白银 Silver 题目描述 Description 已知一个二叉树,判断它是否为二叉堆(小根堆) 输入描述 Input ...
- 数据结构图文解析之:二叉堆详解及C++模板实现
0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...
- POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆
考虑到数据结构短板严重,从计算几何换换口味= = 二叉堆 简介 堆总保持每个节点小于(大于)父亲节点.这样的堆被称作大根堆(小根堆). 顾名思义,大根堆的数根是堆内的最大元素. 堆的意义在于能快速O( ...
- 二叉堆(一)之 图文解析 和 C语言的实现
概要 本章介绍二叉堆,二叉堆就是通常我们所说的数据结构中"堆"中的一种.和以往一样,本文会先对二叉堆的理论知识进行简单介绍,然后给出C语言的实现.后续再分别给出C++和Java版本 ...
- 二叉堆(二)之 C++的实现
概要 上一章介绍了堆和二叉堆的基本概念,并通过C语言实现了二叉堆.本章是二叉堆的C++实现. 目录1. 二叉堆的介绍2. 二叉堆的图文解析3. 二叉堆的C++实现(完整源码)4. 二叉堆的C++测试程 ...
- 二叉堆(三)之 Java的实现
概要 前面分别通过C和C++实现了二叉堆,本章给出二叉堆的Java版本.还是那句话,它们的原理一样,择其一了解即可. 目录1. 二叉堆的介绍2. 二叉堆的图文解析3. 二叉堆的Java实现(完整源码) ...
随机推荐
- shell 脚本大文件处理
shell 脚本大文件处理 字符串处理 s='{"_id":{"$oid":"59b73d80930c17474f9f050d"},&qu ...
- checkbox是否选中判断
三种方式: $('#checkAll').bind('click',function(){ //第一种 console.log(this.checked); //第二种 console.log($(t ...
- HDU 5862Counting Intersections
Counting Intersections Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- openstack 杂记 备忘002
- 72. js EXTJS grid renderer用法
转自:https://blog.csdn.net/shancunxiaoyazhi/article/details/22156083 renderer : Function (可选的)该函数用于加工单 ...
- java静态代理实例
package test; class ProxyTest { public static void main(String[] args) { ProxyClass proxy = new Prox ...
- Oracle查询列重命名
select count(*) 呼入量 from crm_cisco_call_detail
- ios-判断手机上是否安装了某个App
方法一 1.获取手机中安装的所有App 1.1.runtime中的方法,所以要导入 #include <objc/runtime.h> 1.2.在 AppDel ...
- python--修改默认递归层级
import sys sys.setrecursionlimit(最大递归次数)
- ACM_Reverse Bits(反转二进制)
Reverse Bits Time Limit: 2000/1000ms (Java/Others) Problem Description: Reverse bits of a given 32 b ...