6-8 Percolate Up and Down(20 分)
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 分)的更多相关文章
- 抛弃EF,20分构建一个属于自己的ORM框架
Poiuyt_cyc 博客园首页新随笔联系订阅管理随笔 - 11 文章 - 0 评论 - 111 抛弃EF,20分构建一个属于自己的ORM框架 相信EF大家都不陌生了,因为数据库表跟程序实体是一一 ...
- PTA 邻接表存储图的广度优先遍历(20 分)
6-2 邻接表存储图的广度优先遍历(20 分) 试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(V ...
- #020PAT 没整明白的题L1-009 N个数求和 (20 分)
后面的测试点过不去,两个错误一个超时. 目前未解决 L1-009 N个数求和 (20 分) 本题的要求很简单,就是求N个数字的和.麻烦的是,这些数字是以有理数分子/分母的形式给出的,你输出的和 ...
- L1-023 输出GPLT (20 分)
L1-023 输出GPLT (20 分) 给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按GPLTGPLT....这样的顺序输出,并忽略其它字符.当然,四种字符(不区 ...
- PAT 乙级 1074 宇宙无敌加法器 (20 分)
1074 宇宙无敌加法器 (20 分) 地球人习惯使用十进制数,并且默认一个数字的每一位都是十进制的.而在 PAT 星人开挂的世界里,每个数字的每一位都是不同进制的,这种神奇的数字称为“PAT数”.每 ...
- PAT 乙级 1044 火星数字 (20 分)
1044 火星数字 (20 分) 火星人是以 13 进制计数的: 地球人的 0 被火星人称为 tret. 地球人数字 1 到 12 的火星文分别为:jan, feb, mar, apr, may, j ...
- PAT 甲级 1035 Password (20 分)
1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...
- 获取数值型数组中大于60的元素个数,给数值型数组中不足60分的加20分。(数组,for循环,if条件判断语句)
package com.Summer_0420.cn; /** * @author Summer * 获取数值型数组中大于60的元素个数 * 给数值型数组中不足60分的加20分 */ public c ...
- PAT 甲级 1041 Be Unique (20 分)
1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...
- PAT 甲级 1054 The Dominant Color (20 分)
1054 The Dominant Color (20 分) Behind the scenes in the computer's memory, color is always talked ab ...
随机推荐
- Specifications查询
Spring Data JPA支持JPA2.0的Criteria查询,相应的接口是JpaSpecificationExecutor. Criteria 查询:是一种类型安全和更面向对象的查询 这个接口 ...
- 问题:ORA-28000: the account is locked 用户锁住了。
打开cmd: 执行 sqlplus /nolog 执行conn / as sysdba 执行 ALTER USER username ACCOUNT UNLOCK;
- 系统管理命令之tty
在Linux操作系统中,所有外围设备都有其名称与代号,这些名称代号以特殊文件的类型存放于/dev目录下.你可以执行tty指令查询目前使用的终端机的文件名称. 1.命令帮助: # tty -- help ...
- 587. Erect the Fence(凸包算法)
问题 给定一群树的坐标点,画个围栏把所有树围起来(凸包). 至少有一棵树,输入和输出没有顺序. Input: [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]] Output: ...
- TED #04#
Christopher Ategeka: How adoption worked for me 1. I experienced all the negative effects of poverty ...
- Ubuntu18.04 怎么开热点
先说明,电脑上要有wifi适配器,而且连接wifi时,不能开热点 我的笔记本是双系统,现在介绍一下我的设置 在设置里打开热点这个应该会吧,但是热点密码不是自己设置的,而是随机生成的,本文重点介绍一下怎 ...
- CEF禁止右键菜单
转载:http://www.cctry.com/thread-258549-1-1.html 转载:http://blog.sina.com.cn/s/blog_dad2c54101019cmo.ht ...
- 【Java----统计字符串匹配个数】
org.apache.commons.lang3.StringUtils包带的工具类 StringUtils.countMatches(context, keyword);
- fiddler抓包https
http://blog.csdn.net/idlear/article/details/50999490 charles也能抓取https请求 http://blog.csdn.net/jiadoud ...
- bzoj 1799: [Ahoi2009]self 同类分布 数位dp
1799: [Ahoi2009]self 同类分布 Time Limit: 50 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Descripti ...