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

[ZJ] d713 - 中位数

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

    [LV.6]常住居民II

    176

    主題

    612

    帖子

    3959

    積分

    管理員

    Rank: 9Rank: 9Rank: 9

    積分
    3959

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

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

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

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

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

    動態中位數,使用兩個Priority Queue來維護前後各一半的數列,使中位数浮到Priority Queue頂端。


    使STL - Priority queue 能改為min-heap的作法在此是把比序的less改成greater,就可以顛倒順序。

    (tmp)標籤測試,超噁心的方法終於成功了 ...(tag cpp)
    /**********************************************************************************/
    /*  Problem: d713 "中位数" from ACM 10107 c010:What is the Median? 加强版   */
    /*  Language: CPP (666 Bytes)                                                     */
    /*  Result: AC(0.1s, 3.8MB) judge by this@ZeroJudge                               */
    /*  Author: lfs92002 at 2013-08-08 09:31:24                                       */
    /**********************************************************************************/


    #include<cstdio>
    #include<queue>
    #include<functional>
    using namespace std;
    #define LL long long
    int main()
    {
            priority_queue<LL,vector<LL>,greater<LL> > Mh;
            priority_queue<LL> mh;
            int i;
            while(~scanf("%lld",&i))
            {
                    if(mh.size()==0)
                            mh.push(i);
                    else if(mh.top()>i)
                            mh.push(i);
                    else
                            Mh.push(i);
                    if(mh.size()>Mh.size()+1)
                    {
                            Mh.push(mh.top());
                            mh.pop();
                    }
                    else if(mh.size()+1<Mh.size())
                    {
                            mh.push(Mh.top());
                            Mh.pop();
                    }
                    if(mh.size()==Mh.size())
                            printf("%lld\n",(mh.top()+Mh.top())/2);
                    else if(mh.size()>Mh.size())
                            printf("%lld\n",mh.top());
                    else
                            printf("%lld\n",Mh.top());
            }
    }



    點評

    原來這題是這樣寫的!  發表於 2014-5-7 07:51
    回復

    使用道具 檢舉

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

    本版積分規則

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