6-8 Percolate Up and Down(20 分)

Posted 给杰瑞一块奶酪~

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 = 1;
    while(flag)
    {
        if(p/2 && H -> Elements[p] <= H -> Elements[p/2])
        {
            int d = H -> Elements[p];
            H -> Elements[p] = H -> Elements[p/2];
            H -> Elements[p/2] = d;
            p = p/2;
        }
        else flag = 0;
    }
}
void PercolateDown( int p, PriorityQueue H )
{
    int flag = 1;
    int t;
    while(flag)
    {
        if(p * 2 <= H -> Size && H -> Elements[p] >= H -> Elements[p*2])
        {
            t = p*2;
        }
        else t = p;
        if(p * 2 + 1 <= H -> Size && H ->Elements[t] >= H -> Elements[p*2 + 1])
        {
            t = p * 2 + 1;
        }
        if(p != t)
        {
            int d = H ->Elements[t];
            H ->Elements[t] = H -> Elements[p];
            H -> Elements[p] = d;
            p = t;
        }
        else flag = 0;
    }
}

 

以上是关于6-8 Percolate Up and Down(20 分)的主要内容,如果未能解决你的问题,请参考以下文章

只记得其中一句歌词是 Baby play up and down 这首歌的名字是啥?急(走秀用过!)

codeforces 653C C. Bear and Up-Down(乱搞题)

CF1065F Up and Down the Tree

Codeforces1065F Up and Down the Tree 树形DP

CF1065F Up and Down the Tree

[CF1065F]Up and Down the Tree