码迷,mamicode.com
首页 > 其他好文 > 详细

PTA Percolate Up and Down

时间:2016-11-01 00:58:40      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:tput   lang   min   creat   max   function   axel   class   priority   

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

解答:

这题真的是好水啊……而且那个输入输出样例有必要给出么……直接说写最小堆的插入和删除操作就好了……都是常规操作啊
//
//  main.c
//  Percolate Up and Down
//
//  Created by 余南龙 on 2016/10/31.
//  Copyright ? 2016年 余南龙. All rights reserved.
//

void PercolateUp( int p, PriorityQueue H ){
    ElementType tmp = H->Elements[p];
    
    while(p / 2 >= 1&&H->Elements[p / 2] > tmp){
        H->Elements[p] = H->Elements[p / 2];
        p = p / 2;
    }
    H->Elements[p] = tmp;
}
void PercolateDown( int p, PriorityQueue H ){
    ElementType tmp = H->Elements[p];
    
    while(1){
        if(p * 2 + 1 <= H->Size){
            if(H->Elements[p * 2 + 1] < H->Elements[p * 2]&&H->Elements[p * 2 + 1] < tmp){
                H->Elements[p] = H->Elements[p * 2 + 1];
                p = p * 2 + 1;
            }
            else if(H->Elements[p * 2 + 1] > H->Elements[p * 2]&&H->Elements[p * 2] < tmp){
                H->Elements[p] = H->Elements[p * 2];
                p = p * 2;
            }
            else{
                break;
            }
        }
        else if(p * 2 <= H->Size){
            if(H->Elements[p * 2] < tmp){
                H->Elements[p] = H->Elements[p * 2];
                p = p * 2;
            }
            else{
                break;
            }
        }
        else{
            break;
        }
    }
    H->Elements[p] = tmp;
}

 



PTA Percolate Up and Down

标签:tput   lang   min   creat   max   function   axel   class   priority   

原文地址:http://www.cnblogs.com/YuNanlong/p/6017961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!