#include<stdio.h>
#include<stdlib.h>
#include<cstdio>
#include<iostream>
#include<stack>
#include<queue>
#include<string.h>
using namespace std;
#define Min -10001
int t;
typedef struct Heapstruct *MinHeap;
struct Heapstruct
{
int *Elements;
int Size;
};
void Insert( MinHeap H, int a )
{
int i;
i=++H->Size;
for( ; H->Elements[i/2]>a && i >= 1; i/=2 )
H->Elements[i] = H->Elements[i/2];
H->Elements[i] = a;
}
void Print( MinHeap H, int a )
{
for( int i = a; i >= 1; i/=2 )
{
if(t==1)
printf("%d",H->Elements[i]);
else
printf(" %d",H->Elements[i]);
t++;
}
printf("\n");
}
MinHeap CreatMinHeap(int n )
{
MinHeap H = ( struct Heapstruct * )malloc( sizeof( struct Heapstruct));
H->Elements = ( int * )malloc((n+1)*sizeof(int));
H->Size = 0;
H->Elements[0] = Min;
return H;
}
int main()
{
MinHeap H;
int m,n,a;
scanf("%d%d",&m,&n);
H=CreatMinHeap(m);
for( int i = 1; i <= m; i++ )
{
scanf("%d",&a);
Insert(H,a);
}
for( int i = 1; i<=n; i++ )
{
scanf("%d",&a);
t=1;
Print(H,a);
}
return 0;
}
堆中的路径
最新推荐文章于 2020-01-22 00:19:31 发布