查看: 2204|回復: 1
打印 上一主題 下一主題

[ZJ] a091 - 今晚打老虎

[複製鏈接]
  • TA的每日心情
    慵懶
    2015-4-10 14:18
  • 簽到天數: 78 天

    [LV.6]常住居民II

    176

    主題

    612

    帖子

    3959

    積分

    管理員

    Rank: 9Rank: 9Rank: 9

    積分
    3959

    台南一中資訊社新手達陣程式設計達人 - 2014

    跳轉到指定樓層
    樓主
    發表於 2014-5-6 13:35:42 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式

    趕快加入我們來參與討論吧!

    您需要 登錄 才可以下載或查看,沒有帳號?加入我們

    x
    原題:http://zerojudge.tw/ShowProblem?problemid=a091
    AC:http://zerojudge.tw/Submissions?problemid=a091&account=lfs92002

    SMMH的實作題,聽說可以用set來爆我還沒試過,不知道會不會TLE

    [C++] 純文本查看 復制代碼
    /**********************************************************************************/
    /*  Problem: a091 "今晚打老虎" from Deap | min-max heap | double-ended heap  */
    /*  Language: CPP (2601 Bytes)                          */
    /*  Result: AC(0.8s, 4.2MB) judge by this@ZeroJudge                 */
    /*  Author: lfs92002 at 2014-03-03 09:40:37                     */
    /**********************************************************************************/
    
    
    #include<cstdio>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    
    #define ROOT 0
    #define PA(X) (((X)-1)/2)
    #define LC(X) ((X)*2+1)
    #define RC(X) ((X)*2+2)
    #define GP(X) PA(PA(X))
    
    int heap[1000005];
    int size=0;
    
    void insert(int var)
    {
        int pos=++size;
        heap[pos]=var;
        //step1
        if(pos%2==0) //is right-child
        {
            if(heap[pos-1]>heap[pos])
            {
                swap(heap[pos-1],heap[pos]);
                pos--;
            }
        }
        while(pos>2)
        {
            int pGL=LC(GP(pos));
            int pGR=RC(GP(pos));
            if(heap[pos]<heap[pGL])
            {
                swap(heap[pos],heap[pGL]);
                pos=pGL;
            }
            else if(heap[pGR]<heap[pos])
            {
                
                swap(heap[pos],heap[pGR]);
                pos=pGR;
            }
            else
            {
                break;
            }
        }
    }
    int pop_max()
    {
        int M=heap[2],pos=2;
        heap[2]=heap[size--];
        while(pos<=size)
        {
            int pR=RC(pos);
            int pPR=RC(LC(PA(pos)));
            
            if(pR>size)pR=-1;
            if(pPR>size)pPR=-1;
            
            if(heap[pos-1]>heap[pos])
            {
                swap(heap[pos-1],heap[pos]);
                continue;
            }
            
            if(pR<0&&pPR<0)break;
            if(pR<0)
            {
                if(heap[pPR]>heap[pos])
                {
                    swap(heap[pPR],heap[pos]);
                    pos=pPR;
                }
                else
                {
                    break;
                }
            }
            else
            {
                int C=(heap[pR]>heap[pPR])?pR:pPR;
                if(heap[C]>heap[pos])
                {
                    swap(heap[C],heap[pos]);
                    pos=C;
                }
                else
                {
                    break;
                }
            }
        }
        if(heap[pos-1]>heap[pos])
        {
            swap(heap[pos-1],heap[pos]);
        }
        return M;
    }
    
    int pop_min()
    {
        int m=heap[1],pos=1;
        heap[1]=heap[size--];
        while(pos<=size)
        {
            int pL=LC(pos);
            int pPL=LC(RC(PA(pos)));
            
            if(pL>size)pL=-1;
            if(pPL>size)pPL=-1;
            if(pos+1<=size)
            {
                if(heap[pos]>heap[pos+1])
                {
                    swap(heap[pos],heap[pos+1]);
                    continue;
                }
            }
            
            if(pL<0 &&pPL<0)break;
            if(pPL<0) //Only pL
            {
                if(heap[pL]<heap[pos])
                {
                    swap(heap[pL],heap[pos]);
                    pos=pL;
                }
                else
                {
                    break;
                }
            }
            else
            {
                int C=(heap[pL]<heap[pPL])?pL:pPL;
                if(heap[C]<heap[pos])
                {
                    swap(heap[C],heap[pos]);
                    pos=C;
                }
                else
                {
                    break;
                }
            }
        }
        if(pos+1<=size)
        {
            if(heap[pos]>heap[pos+1])
                swap(heap[pos],heap[pos+1]);
        }
        return m;
    }
    void show()
    {
        cout<<"SMMH :";
        for(int i=1;i<=size;++i)
        {
            cout<<heap[i]<<' ';
        }
        cout<<endl;
    }
    int main()
    {
        size=0;
        int id;
        while(~scanf("%d",&id))
        {
            if(id==1)
            {
                scanf("%d",&id);
                insert(id);
            }
            else if(size == 1) //only 1 obj
            {
                printf("%d\n",pop_min());
            }
            else if(id==2)
            {
                printf("%d\n",pop_max());
            }
            else
            {
                printf("%d\n",pop_min());
            }
        //    show();
        }
    }




    回復

    使用道具 檢舉

  • TA的每日心情
    慵懶
    2015-4-10 14:18
  • 簽到天數: 78 天

    [LV.6]常住居民II

    176

    主題

    612

    帖子

    3959

    積分

    管理員

    Rank: 9Rank: 9Rank: 9

    積分
    3959

    台南一中資訊社新手達陣程式設計達人 - 2014

    頭香
     樓主| 發表於 2014-5-9 08:39:08 | 只看該作者
    multiset版本,執行時間比較慢,但還是過了,才30多行的code沒天理啊!

    [C++] 純文本查看 復制代碼
    /**********************************************************************************/
    /*  Problem: a091 "今晚打老虎" from Deap | min-max heap | double-ended heap  */
    /*  Language: CPP (527 Bytes)                                                     */
    /*  Result: AC(1s, 23.3MB) judge by this@ZeroJudge                                */
    /*  Author: lfs92002 at 2014-05-09 08:36:42                                       */
    /**********************************************************************************/
    
    
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <set>
    using namespace std;
    
    
    int main()
    {
            multiset<int> s;
            multiset<int>::iterator sit;
            multiset<int>::reverse_iterator srit;
            int D,t;
            while(~scanf("%d",&D)){
                    switch(D){
                            case 1:
                                    scanf("%d",&t);
                                    s.insert(t);
                                    break;
                            case 2:
                                    srit=s.rbegin();
                                    printf("%d\n",*srit);
                                    s.erase((++srit).base());
                                    break;
                            case 3:
                                    sit=s.begin();
                                    printf("%d\n",*sit);
                                    s.erase(sit);
                                    break;
                    }
            }
    }
    
    回復 支持 反對

    使用道具 檢舉

    您需要登錄後才可以回帖 登入 | 加入我們

    本版積分規則

    快速回覆 返回頂部 返回列表