pta Percolate Up and Down
来源:互联网 发布:互联网数据分析师前景 编辑:程序博客网 时间:2024/05/01 09:28
Write the routines to do a “percolate up” and a “percolate down” in a binary min-heap.
Format of functions:
void PercolateUp( int p, PriorityQueue H );void PercolateDown( int p, PriorityQueue H );
where int p is the position of the element, and PriorityQueue is defined as the following:
typedef struct HeapStruct *PriorityQueue;struct HeapStruct { ElementType *Elements; int Capacity; int Size;};
Sample program of judge:
#include <stdio.h>#include <stdlib.h>typedef int ElementType;#define MinData -1typedef struct HeapStruct *PriorityQueue;struct HeapStruct { ElementType *Elements; int Capacity; int Size;};PriorityQueue Initialize( int MaxElements ); /* details omitted */void PercolateUp( int p, PriorityQueue H );void PercolateDown( int p, PriorityQueue H );void Insert( ElementType X, PriorityQueue H ) { int p = ++H->Size; H->Elements[p] = X; PercolateUp( p, H );}ElementType DeleteMin( PriorityQueue H ) { ElementType MinElement; MinElement = H->Elements[1]; H->Elements[1] = H->Elements[H->Size--]; PercolateDown( 1, H ); return MinElement; }int main(){ int n, i, op, X; PriorityQueue H; scanf("%d", &n); H = Initialize(n); for ( i=0; i<n; i++ ) { scanf("%d", &op); switch( op ) { case 1: scanf("%d", &X); Insert(X, H); break; case 0: printf("%d ", DeleteMin(H)); break; } } printf("\nInside H:"); for ( i=1; i<=H->Size; i++ ) printf(" %d", H->Elements[i]); return 0;}/* Your function will be put here */
Sample Input:
91 101 51 201 91 11 400
Sample Output:
2 1 4 Inside H: 5 10 9
思路:
题目让实现最小堆的插入维护和删除维护。只要记住最小的在上面就行。
另外要注意down的时候可能只有一个儿子。
代码:
void PercolateDown( int p, PriorityQueue H ){ if(p<<1 > H->Size)return; int replace; if(p<<1+1 <= H->Size)replace = (H->Elements[p<<1] < H->Elements[p<<1+1] ? p<<1 : p<<1+1); else replace = p<<1; if(H->Elements[replace] < H->Elements[p]) { int mid = H->Elements[replace]; H->Elements[replace] = H->Elements[p]; H->Elements[p] = mid; PercolateDown( replace, H ); }}void PercolateUp( int p, PriorityQueue H ){ if(p == 1)return; if(p&1) { if(H->Elements[(p-1)>>1] > H->Elements[p]) { int mid = H->Elements[(p-1)>>1]; H->Elements[(p-1)>>1] = H->Elements[p]; H->Elements[p] = mid; PercolateUp( (p-1)>>1, H ); } } else { if(H->Elements[p>>1] > H->Elements[p]) { int mid = H->Elements[p>>1]; H->Elements[p>>1] = H->Elements[p]; H->Elements[p] = mid; PercolateUp( p>>1, H ); } }}
阅读全文
0 0
- pta Percolate Up and Down
- Percolate Up and Down
- 4-1 Percolate Up and Down
- 4-8-Percolate Up and Down
- 4-1 Percolate Up and Down (20分)
- 6-13 Percolate Up and Down(20 point(s))
- volume up and down
- UVa413 - Up and Down Sequences
- Top-down and bottom-up parser
- Swapping GridView rows Up and Down
- zoj 1338 Up and Down Sequences
- ZOJ 1338 Up and Down Sequences
- ZOJ 1338 Up and Down Sequences
- ZOJ 1338 Up and Down Sequences
- SZU:A12 Jumping up and down
- Pull-up and Pull-down Resistors
- TOJ 1101. Up and Down Sequences
- UVa 413 - Up and Down Sequences
- LintCode 二叉树的路径和
- [RE]一个RE题
- easyui 与 java 通信问题
- 初中生如何入门编程
- Spring 中常用的几种注解
- pta Percolate Up and Down
- PADS设置敷铜实心铜和网格铜
- 4
- 第八周项目五C/C++稀疏矩阵的三元组表示的实现及应用
- RCNN系列目标检测方法概述
- 在MATLAB的figure图中画局部放大的图中图
- sdut 2506 完美网络(优先队列)
- Linux之Mysql
- Kafka:Producer Config 配置