现在的位置: 首页 > 综合 > 正文

二叉堆小结

2019年08月21日 ⁄ 综合 ⁄ 共 4529字 ⁄ 字号 评论关闭

二叉堆基本操作:(可用优先队列模板)

1.上升操作(可以用于插入,并不等于插入操作)

2.下降操作(可以用于删除,并不等于删除操作)

3.(知道了 1和2操作)要知道怎么删除堆内点!

4.堆排序

二叉堆 (小堆与大堆的合并运用)典例

poj1442

Black Box
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 6131   Accepted: 2486

Description

Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empty and i equals 0. This Black Box processes a sequence of commands (transactions). There are two types of transactions: 

ADD (x): put element x into Black Box; 
GET: increase i by 1 and give an i-minimum out of all integers containing in the Black Box. Keep in mind that i-minimum is a number located at i-th place after Black Box elements sorting by non- descending. 

Let us examine a possible sequence of 11 transactions: 

Example 1 

N Transaction i Black Box contents after transaction Answer 

      (elements are arranged by non-descending)   

1 ADD(3)      0 3   

2 GET         1 3                                    3 

3 ADD(1)      1 1, 3   

4 GET         2 1, 3                                 3 

5 ADD(-4)     2 -4, 1, 3   

6 ADD(2)      2 -4, 1, 2, 3   

7 ADD(8)      2 -4, 1, 2, 3, 8   

8 ADD(-1000)  2 -1000, -4, 1, 2, 3, 8   

9 GET         3 -1000, -4, 1, 2, 3, 8                1 

10 GET        4 -1000, -4, 1, 2, 3, 8                2 

11 ADD(2)     4 -1000, -4, 1, 2, 2, 3, 8   

It is required to work out an efficient algorithm which treats a given sequence of transactions. The maximum number of ADD and GET transactions: 30000 of each type. 

Let us describe the sequence of transactions by two integer arrays: 

1. A(1), A(2), ..., A(M): a sequence of elements which are being included into Black Box. A values are integers not exceeding 2 000 000 000 by their absolute value, M <= 30000. For the Example we have A=(3, 1, -4, 2, 8, -1000, 2). 

2. u(1), u(2), ..., u(N): a sequence setting a number of elements which are being included into Black Box at the moment of first, second, ... and N-transaction GET. For the Example we have u=(1, 2, 6, 6). 

The Black Box algorithm supposes that natural number sequence u(1), u(2), ..., u(N) is sorted in non-descending order, N <= M and for each p (1 <= p <= N) an inequality p <= u(p) <= M is valid. It follows from the fact that for the p-element of our u sequence
we perform a GET transaction giving p-minimum number from our A(1), A(2), ..., A(u(p)) sequence. 

Input

Input contains (in given order): M, N, A(1), A(2), ..., A(M), u(1), u(2), ..., u(N). All numbers are divided by spaces and (or) carriage return characters.

Output

Write to the output Black Box answers sequence for a given sequence of transactions, one number each line.

Sample Input

7 4
3 1 -4 2 8 -1000 2
1 2 6 6

Sample Output

3
3
1
2

Source

题解:

案例解析:

输入 M,N 分别是A数组,u数组的大小。

然后是一行A数组值,一行u数组值。

按照u数组开始,如u[1]=1  就要从A的前 u[1] 个数里面选出第1(这个数字是u下标)小的值。->即输出3.

同理 看u[3]=6, 就找 A前u[3](即6)个数中 第3小的值。即输出1;

思考:

我们这么想:如果让你找第n小的。那么用小堆是不太好取到的,那么反着想用大堆,正好大堆从底开始是 第一小的,第二小的·······到 堆顶 刚好是 第n小的。所以用大堆方便

取出所要的值。(取堆顶就OK了)。那么大堆用永远只有 n-1个值,然后通过小堆插入一个进来,再选出当前第n小的值。所以说每次的新插入数据,都是在选出当前最新

的第n小的值!

下面看怎么解:

即维护 一个小堆 一个大堆;

每次用大堆来选出第n小的数,插入小堆堆顶(其实这个插入不用维护,插入的一定是小堆堆顶(即第n小的数))

然后小堆里的 都是通过大堆选出来的  第n小的数。那么最后输出时,就要从这么多第n小的数中 选出最小的。(其实就是最后一次从大堆取出来的插入小堆的数)

//体会一遍 那个用 大堆小堆的 过程吧!就会明白!

//Accepted	600K	141MS	C++	2242B
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAX 30010
int M,N;
int A[MAX];
int u[MAX];
int m[MAX];//小堆
int x[MAX];//大堆
int m_num,x_num;//小堆,大堆的数据个数
void swap(int &a,int &b)
{
    int t=a;a=b;b=t;
}
/**小堆**/
void adjust_min()/**下降操作(这里只需要从顶点维护)**/
{
    int lg,lr;
    int x=1;
    while((x<<1)<=m_num)
    {
        lg=x<<1;
        lr=lg+1;
        if(lr<=m_num&&m[lr]<m[lg])
            lg=lr;
        if(m[lg]<m[x])
        {
            swap(m[lg],m[x]);
            x=lg;
        }
        else break;
    }
}
void push_min(int x) /**上升操作(插入)**/
{
    int i=++m_num;
    m[m_num]=x;
    while(i>1&&m[i/2]>x)
    {
        m[i]=m[i/2];
        i/=2;
    }
    m[i]=x;
}
int top_min()/**得到小堆顶**/
{
    return m[1];
}
void pop_min() /**删除小堆顶**/
{
    swap(m[1],m[m_num]);
    m_num--;
    adjust_min();
}
/**大堆(以下操作,类似于小堆.)**/  
void adjust_max()
{
    int lg,lr;
    int key=1;
    while((key<<1)<=x_num)
    {
        lg=key<<1;
        lr=lg+1;
        if(lr<=x_num&&x[lr]>x[lg])
            lg=lr;
        if(x[lg]>x[key])
        {
            swap(x[lg],x[key]);
            key=lg;
        }
        else break;
    }
}
void push_max(int key)
{
    int i=++x_num;
     x[x_num]=key;
    while(i>1&&x[i/2]<key)
    {
        x[i]=x[i/2];
        i/=2;
    }
    x[i]=key;
}
int top_max()
{
    return x[1];
}
void pop_max()
{
    swap(x[1],x[x_num]);
    x_num--;
    adjust_max();
}
/**主函数**/
int main()
{
    while(~scanf("%d%d",&M,&N))
    {
        /**初始化堆**/
        memset(m,0,sizeof(m));
        memset(x,0,sizeof(x));
        m_num=0;
        m_num=0;
        for(int i=1;i<=M;i++)
            scanf("%d",&A[i]);
        for(int i=1;i<=N;i++)
            scanf("%d",&u[i]);

        int add_index=1;
        for(int i=1;i<=N;i++)
        {
            for(;add_index<=u[i];add_index++)
            {
                push_min(A[add_index]);
                push_max(top_min());
                pop_min();
                push_min(top_max());
                pop_max();
            }
            printf("%d\n",top_min());
            push_max(top_min());
            pop_min();
        }

    }
    return 0;
}

优先队列

#include <iostream>
#include<cstdio>
#include<queue>
using namespace std;
#define MAX 30010
int A[MAX],u[MAX];
int m,n;
int main()
{
    priority_queue<int,vector<int>,greater<int> > Min;
    priority_queue<int> Max;
    while(~scanf("%d%d",&m,&n))
    {
        for(int i=1;i<=m;i++)
            scanf("%d",&A[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&u[i]);
        int add_x=1;
        for(int i=1;i<=n;i++)
        {
            for(;add_x<=u[i];add_x++)//max 是插一次删一次没变,因为它要保持堆顶为第i小!
            {                        //min 是插删插 增了一个,会选出很多个第n小到小堆,维护第n小。
                Min.push(A[add_x]);
                Max.push(Min.top());
                Min.pop();
                Min.push(Max.top());
                Max.pop();
            }
            printf("%d\n",Min.top());
            Max.push(Min.top());
            Min.pop();
        }
    }
    return 0;
}

抱歉!评论已关闭.