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 -1 typedef 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:

9
1 10
1 5
1 2
0
1 9
1 1
1 4
0
0

Sample Output:

2 1 4
Inside H: 5 10 9
代码:
void PercolateUp( int p, PriorityQueue H )
{
int flag = ;
while(flag)
{
if(p/ && H -> Elements[p] <= H -> Elements[p/])
{
int d = H -> Elements[p];
H -> Elements[p] = H -> Elements[p/];
H -> Elements[p/] = d;
p = p/;
}
else flag = ;
}
}
void PercolateDown( int p, PriorityQueue H )
{
int flag = ;
int t;
while(flag)
{
if(p * <= H -> Size && H -> Elements[p] >= H -> Elements[p*])
{
t = p*;
}
else t = p;
if(p * + <= H -> Size && H ->Elements[t] >= H -> Elements[p* + ])
{
t = p * + ;
}
if(p != t)
{
int d = H ->Elements[t];
H ->Elements[t] = H -> Elements[p];
H -> Elements[p] = d;
p = t;
}
else flag = ;
}
}

6-8 Percolate Up and Down(20 分)的更多相关文章

  1. 抛弃EF,20分构建一个属于自己的ORM框架

    Poiuyt_cyc 博客园首页新随笔联系订阅管理随笔 - 11  文章 - 0  评论 - 111 抛弃EF,20分构建一个属于自己的ORM框架 相信EF大家都不陌生了,因为数据库表跟程序实体是一一 ...

  2. PTA 邻接表存储图的广度优先遍历(20 分)

    6-2 邻接表存储图的广度优先遍历(20 分) 试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(V ...

  3. #020PAT 没整明白的题L1-009 N个数求和 (20 分)

    后面的测试点过不去,两个错误一个超时. 目前未解决   L1-009 N个数求和 (20 分)   本题的要求很简单,就是求N个数字的和.麻烦的是,这些数字是以有理数分子/分母的形式给出的,你输出的和 ...

  4. L1-023 输出GPLT (20 分)

    L1-023 输出GPLT (20 分) 给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按GPLTGPLT....这样的顺序输出,并忽略其它字符.当然,四种字符(不区 ...

  5. PAT 乙级 1074 宇宙无敌加法器 (20 分)

    1074 宇宙无敌加法器 (20 分) 地球人习惯使用十进制数,并且默认一个数字的每一位都是十进制的.而在 PAT 星人开挂的世界里,每个数字的每一位都是不同进制的,这种神奇的数字称为“PAT数”.每 ...

  6. PAT 乙级 1044 火星数字 (20 分)

    1044 火星数字 (20 分) 火星人是以 13 进制计数的: 地球人的 0 被火星人称为 tret. 地球人数字 1 到 12 的火星文分别为:jan, feb, mar, apr, may, j ...

  7. PAT 甲级 1035 Password (20 分)

    1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...

  8. 获取数值型数组中大于60的元素个数,给数值型数组中不足60分的加20分。(数组,for循环,if条件判断语句)

    package com.Summer_0420.cn; /** * @author Summer * 获取数值型数组中大于60的元素个数 * 给数值型数组中不足60分的加20分 */ public c ...

  9. PAT 甲级 1041 Be Unique (20 分)

    1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...

  10. PAT 甲级 1054 The Dominant Color (20 分)

    1054 The Dominant Color (20 分) Behind the scenes in the computer's memory, color is always talked ab ...

随机推荐

  1. SpringData_PagingAndSortingRepository接口

    该接口提供了分页与排序功能 Iterable<T> findAll(Sort sort); //排序 Page<T> findAll(Pageable pageable); / ...

  2. Spring整合Mybatis 之分页插件使用

    [分页插件项目中的正式代码一共有个5个Java文件,这5个文件的说明如下] Page<E>[必须]:分页参数类,该类继承ArrayList,虽然分页查询返回的结果实际类型是Page< ...

  3. CCPC-Wannafly Winter Camp Day5 (Div2, onsite)

    Replay: Dup4: 时间复杂度算不对? 一点点思路不经过验证就激动的要死? 浪费自己一个小时还浪费别人一个小时? 对1e3不敏感? 1e3 * 1e3是多少? 模拟建边跑dp不写非要写个大模拟 ...

  4. hdu5091 线段树

    题意: 给了n个点在平面中 n<10000  然后 将这给了一个 宽为W 高为 H 的 矩形, 然后 使得这个矩形可以 涵盖最多的点有多少个,然后矩形的宽平行x 轴高平行y轴.可以将该矩形 水平 ...

  5. ng-深度学习-课程笔记-6: 建立你的机器学习应用(Week1)

    1 训练/验证/测试集( Train/Dev/test sets ) 构建神经网络的时候有些参数需要选择,比如层数,单元数,学习率,激活函数.这些参数可以通过在验证集上的表现好坏来进行选择. 前几年机 ...

  6. ng-深度学习-课程笔记-9: 机器学习策略1(Week1)

    1 为什么要应用机器学习策略( Why is machine learning strategy ) 当你想优化一个问题的时候,通常可以有很多尝试(比如收集更多数据,增加迭代次数,改用adam,改变网 ...

  7. chrome不能安装adblock插件

    csdn简直就是个垃圾,名字山寨MSDN不说,一个页面数还十个广告.国人还这么多人捧,真是醉了.博客的话还是博客园,简洁,一切为了技术. 既然csdn是个垃圾,那么看部分文章时怎么少得了广告屏蔽插件a ...

  8. RocEDU.阅读.写作《苏菲的世界》书摘(七)

    * 康德认为"事物本身"和"我眼中的事物"是不一样的.这点很重要.我们永远无法确知事物"本来"的面貌.我们所知道的只是我们眼中"看 ...

  9. VMware Vcenter Server 6.0忘记密码

    windows 版Vcenter6.0 重置密码首先要登录到安装Vcenter的windows服务器上 用管理员身份打开CMD命令行 进入VMware VCenter的目录 默认是C:\Program ...

  10. 100W数据,测试复合索引

    复合索引不是那么容易被catch到的. 两个查询条件都是等于的时候,才会被catch到. mysql> select count(*) from tf_user_index where sex ...