二叉堆小結

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

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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章