24h購物| | PChome| 登入
2011-04-07 13:08:24| 人氣2,432| 回應0 | 上一篇 | 下一篇

Deap (Double-Ended heap)

推薦 0 收藏 0 轉貼0 訂閱站台

/**********************************************************************************/
/*  Problem: a091 "今晚打老虎" from ???? | ????                              */
/*  Language: C                                                                   */
/*  Result: AC (464ms, 1048KB) on ZeroJudge                                       */
/*  Author: morris1028 at 2011-04-06 22:14:20                                     */
/**********************************************************************************/


#include<stdio.h>
#include<stdlib.h>
struct Deap{
    int V;
}Deap[1000001], T;
int L=1;
 
int Judge(int p) {
    while(p > 3)    p/=2;
    return p; /*2 left-min heap 3 right-max heap*/
}
int MinPartner(int p) {
    int i=p, s=1;
    while(p > 3)    p/=2, s*=2;
    return i-s;
}
int MaxPartner(int p) {
    if(p == 2) return 2;
    int i=p, s=1;
    while(p > 3)    p/=2, s*=2;
    if(i+s > L) return (i+s)/2;
    return i+s;
}
void MinInsert(int S, int N) {
    int P=S/2;
    while(P > 1 && Deap[P].V > N)   Deap[S].V=Deap[P].V, S=P, P=S/2;
    Deap[S].V=N;
}
void MaxInsert(int S, int N) {
    int P=S/2;
    while(P > 1 && Deap[P].V < N)   Deap[S].V=Deap[P].V, S=P, P=S/2;
    Deap[S].V=N;
}
void Deap_Insert(int N) {
    L++;
    if(L==2) {
        Deap[2].V=N;
        return;
    }
    int p=L, i;
    switch( Judge(p) ) {
            case 3:/*right */
                i=MinPartner(p);
                if(N < Deap[i].V)     Deap[p].V=Deap[i].V, MinInsert(i, N);
                else MaxInsert(p, N);
                break;
            case 2:/*left */
                i=MaxPartner(p);
                if(N > Deap[i].V)     Deap[p].V=Deap[i].V, MaxInsert(i, N);
                else MinInsert(p, N);
                break;
        }
}
void Delete_Max()
{
    int p=L, t=Deap[L--].V, a, b, i;
    for(a = 3; a*2 <= L; a = b) {
            a*=2;
            if(a < L && Deap[a].V < Deap[a+1].V)      b=a+1;
            else b=a;
            Deap[a/2].V=Deap[b].V;
        }
       
    i= MinPartner(a);
    int biggest=i;
    if(2*i <= L) {
            biggest=2*i;
            if (((2*i + 1)<=L) && (Deap[2*i].V < Deap[2*i+1].V))
                biggest++;
        }
    if(t < Deap[biggest].V) {
            Deap[a].V=Deap[biggest].V;
            MinInsert(biggest, t);
        }
    else MaxInsert(a, t);
}
void Delete_Min()
{
    int p=L, t=Deap[L--].V, a, b, i;
    for(a = 2; a*2<=L; a = b) {
            a*=2;
            if(a < L && Deap[a].V > Deap[a+1].V)
                b=a+1;
            else b=a;
            Deap[a/2].V=Deap[b].V;
        }
    i= MaxPartner(a);
    if(t > Deap[i].V) {
            Deap[a]=Deap[i];
            MaxInsert(i, t);
        }
    else MinInsert(a, t);
}
main() {
    static int D, N, a;
    while(scanf("%d",&D)==1) {
            switch(D) {
                    case 1:scanf("%d",&N), Deap_Insert(N);break;
                    case 2:printf("%d\n", (L==2) ? Deap[2].V : Deap[3].V );
                            if(L==2) Delete_Min();
                            else Delete_Max();break;
                    case 3:printf("%d\n", Deap[2].V), Delete_Min();break;
                }
        }
    return 0;
}
 

台長: Morris
人氣(2,432) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: 各類演算法與示範題目 |
此分類下一篇:RMQ,Segment Tree(ST)

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文