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 ...
随机推荐
- Educational Codeforces Round 55 (Rated for Div. 2) Solution
A. Vasya and Book Solved. 三种方式取$Min$ #include <bits/stdc++.h> using namespace std; #define ll ...
- 【运维技术】CentOS7上从零开始安装LAMP安装织梦DedeCMS教程
前期准备数据 centos7 系统 安装 appache httpd # 更新httpd yum update httpd # 安装httpd yum install -y httpd # 启动服务 ...
- [one day one question] express 不缓存如何实现
问题描述: express 默认缓存,这怎么破? 解决方案: apiRoutes.use(function (req, res, next) { res.setHeader('Cache-Contro ...
- 反射调用发生错误信息 LoadNeither
错误信息: Service cannot be started. System.Reflection.TargetInvocationException: Exception has been thr ...
- 简单线性dp
小zc现在有三个字符串,他想知道前两个字符串能不能生成第三个字符串,生成规则如下:第一个串的每个字符都可以往第二个串的任意位置插入(包括首尾位置),但必须保证来源于第一个串中的字符在生成后的串中的相对 ...
- NO.1 在Eclipse中安装Maven插件安装详解
前言 本来是没打算写博客的,作为一个13年毕业的菜鸟,自认为水平太渣写不出什么好文章,但是前些日子看到一篇鼓励性质的文章说,技术人员的成长靠的就是点点滴滴的积累,博客内容不一定包含多么高深的内容,但是 ...
- Python学习札记(二十二) 函数式编程3 filter & SyntaxError: unexpected EOF while parsing
参考: filter Problem SyntaxError: unexpected EOF while parsing 遇到该语法错误,一般是由于 括号不匹配 问题. Note 1.filter 用 ...
- 图像等比例缩放的函数封装(PHP)
<?php //图像等比例缩放函数 /** *等比例缩放函数(以保存新图片的方式实现) *@param string $picname 被缩放的处理图片源 *@param int $maxx 缩 ...
- codeforces 256 div2 C. Painting Fence 分治
C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...
- vnc viewer 点击system 卡死现象
转自:http://zhangjunli177.blog.163.com/blog/static/1386073082012103052527557/ VNC viewer desktop dead ...