#includeusingnamespacestd;intmain(){freopen("h.in","r",stdin);freopen("h.ans","w",stdout);intn;while(scanf("%d",&n)&&n){multisetbills;intsum=0;for(inti=0;i

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Hoax or what

系統 1824 0

Hoax or what

題意是詢問一個動態序列的最小值和最大值。

可以用multiset來實現。

      #include <stdio.h>
      
        

#include 
      
      <
      
        set
      
      >


      
        using
      
      
        namespace
      
      
         std;




      
      
        int
      
      
         main() {

    freopen(
      
      
        "
      
      
        h.in
      
      
        "
      
      , 
      
        "
      
      
        r
      
      
        "
      
      
        , stdin);

    freopen(
      
      
        "
      
      
        h.ans
      
      
        "
      
      , 
      
        "
      
      
        w
      
      
        "
      
      
        , stdout);

    
      
      
        int
      
      
         n;

    
      
      
        while
      
       (scanf(
      
        "
      
      
        %d
      
      
        "
      
      , &n) &&
      
         n) {

        multiset
      
      <
      
        int
      
      >
      
         bills;

        
      
      
        int
      
       sum = 
      
        0
      
      
        ;

        
      
      
        for
      
       (
      
        int
      
       i = 
      
        0
      
      ; i < n; i++
      
        ) {

            
      
      
        int
      
      
         cnt;

            scanf(
      
      
        "
      
      
        %d
      
      
        "
      
      , &
      
        cnt);

            
      
      
        for
      
       (
      
        int
      
       j = 
      
        0
      
      ; j < cnt; j++
      
        ) {

                
      
      
        int
      
      
         t;

                scanf(
      
      
        "
      
      
        %d
      
      
        "
      
      , &
      
        t);

                bills.insert(t);

            }



            sum 
      
      += (*bills.rbegin() - *
      
        bills.begin());



            bills.erase(bills.begin());

            bills.erase(
      
      --bills.rbegin().
      
        base
      
      
        ());

        }

        printf(
      
      
        "
      
      
        %d\n
      
      
        "
      
      
        , sum);

    }

}
      
    

這里給出一個最小堆的做法,通過維護一個最小堆,一個最大堆,當push元素時,將這兩個元素連接到一起。當pop最大或者最小的元素時,對應刪除另一個堆中對應的元素。

      #include <stdio.h>
      
        

#include 
      
      <algorithm>
      
        

#include 
      
      <iostream> 


      
        using
      
      
        namespace
      
      
         std;




      
      
        const
      
      
        int
      
       MAXN = 1e6 + 
      
        5
      
      
        ;


      
      
        const
      
      
        int
      
       INF =
      
         1e9;




      
      
        int
      
      
         min_heap[MAXN], max_heap[MAXN];


      
      
        int
      
      
         min_heap_no[MAXN], max_heap_no[MAXN];


      
      
        int
      
      
         min_heap_no_rev[MAXN], max_heap_no_rev[MAXN];




      
      
        class
      
      
         MinHeap {


      
      
        protected
      
      
        :

    
      
      
        int
      
       count, *
      
        heap_;


      
      
        public
      
      
        :

    MinHeap() {}

    MinHeap(
      
      
        int
      
       *heap) : heap_(heap), count(
      
        0
      
      
        ) {}

    
      
      
        void
      
       push(
      
        int
      
      
         x) {

        
      
      ++
      
        count;

        heap_[count] 
      
      =
      
         x;

        up(count);

    }

    
      
      
        int
      
      
         top() {

        
      
      
        return
      
       heap_[
      
        1
      
      
        ];

    }

    
      
      
        void
      
      
         pop() {

        my_swap(
      
      
        1
      
      , count--
      
        );

        heapfy(
      
      
        1
      
      
        );

    }

    
      
      
        /*
      
      
        {{{ del(int i)
      
      
        */
      
      
        void
      
       del(
      
        int
      
      
         i) {

        heap_[i] 
      
      = -
      
        INF; up(i);

        pop();

    }

    
      
      
        /*
      
      
        }}}
      
      
        */
      
      
        virtual
      
      
        void
      
       my_swap(
      
        int
      
       i, 
      
        int
      
      
         j) {

        swap(heap_[i], heap_[j]);

    }

    
      
      
        /*
      
      
        {{{ heapfy(int i) 
      
      
        */
      
      
        void
      
       heapfy(
      
        int
      
      
         i) {

        
      
      
        while
      
       (i <=
      
         count) {

            
      
      
        int
      
       smallest =
      
         i;

            
      
      
        if
      
       (
      
        2
      
       * i <= count && heap_[
      
        2
      
       * i] <
      
         heap_[smallest]) {

                smallest 
      
      = 
      
        2
      
       *
      
         i;

            }

            
      
      
        if
      
       (
      
        2
      
       * i + 
      
        1
      
       <= count && heap_[
      
        2
      
       * i + 
      
        1
      
      ] <
      
         heap_[smallest]) {

                smallest 
      
      = 
      
        2
      
       * i + 
      
        1
      
      
        ;

            }

            
      
      
        if
      
       (i !=
      
         smallest) {

                my_swap(i, smallest);

                i 
      
      =
      
         smallest;

            } 
      
      
        else
      
      
         {

                
      
      
        break
      
      
        ;

            }

        }

    }

    
      
      
        /*
      
      
        }}}
      
      
        */
      
      
        /*
      
      
        {{{ up(int i) 
      
      
        */
      
      
        void
      
       up(
      
        int
      
      
         i) {

        
      
      
        while
      
       (i > 
      
        1
      
      
        ) {

            
      
      
        if
      
       (heap_[i] < heap_[i / 
      
        2
      
      
        ]) {

                my_swap(i, i 
      
      / 
      
        2
      
      
        );

                i 
      
      /= 
      
        2
      
      
        ;

            } 
      
      
        else
      
      
         {

                
      
      
        break
      
      
        ;

            } 

        }

    }

    
      
      
        /*
      
      
        }}}
      
      
        */
      
      
        

};




      
      
        class
      
       MinHeapWithMap : 
      
        public
      
      
         MinHeap {


      
      
        private
      
      
        :

    
      
      
        int
      
       id, *no_, *
      
        no_rev_;


      
      
        public
      
      
        :

    MinHeapWithMap() {}

    MinHeapWithMap(
      
      
        int
      
       *heap, 
      
        int
      
       *no, 
      
        int
      
       *no_rev) : MinHeap(heap), no_(no), no_rev_(no_rev), id(
      
        0
      
      
        ) {}

    
      
      
        void
      
       push(
      
        int
      
      
         x) {

        no_[count 
      
      + 
      
        1
      
      ] =
      
         id;

        no_rev_[no_[count 
      
      + 
      
        1
      
      ]] = count + 
      
        1
      
      
        ;

        id
      
      ++
      
        ;

        MinHeap::push(x);

    }

    
      
      
        virtual
      
      
        void
      
       my_swap(
      
        int
      
       i, 
      
        int
      
      
         j) {

        MinHeap::my_swap(i, j);

        swap(no_[i], no_[j]);

        no_rev_[no_[i]] 
      
      =
      
         i;

        no_rev_[no_[j]] 
      
      =
      
         j;

    }

    
      
      
        int
      
      
         top_no() {

        
      
      
        return
      
       no_[
      
        1
      
      
        ];

    }

    
      
      
        int
      
       no_rev(
      
        int
      
      
         i) {

        
      
      
        return
      
      
         no_rev_[i]; 

    }

};




      
      
        class
      
      
         Solution {


      
      
        private
      
      
        :

    MinHeapWithMap minHeap_, maxHeap_;


      
      
        public
      
      
        :

    Solution() {

        minHeap_ 
      
      =
      
         MinHeapWithMap(min_heap, min_heap_no, min_heap_no_rev);

        maxHeap_ 
      
      =
      
         MinHeapWithMap(max_heap, max_heap_no, max_heap_no_rev);

    }

    
      
      
        void
      
       push(
      
        int
      
      
         x) {

        minHeap_.push(x);

        maxHeap_.push(
      
      -
      
        x);

    }

    
      
      
        int
      
      
         getMaxMinDiff() {

        
      
      
        int
      
       res = -maxHeap_.top() -
      
         minHeap_.top();

        minHeap_.del(minHeap_.no_rev(maxHeap_.top_no()));

        maxHeap_.del(maxHeap_.no_rev(minHeap_.top_no()));

        minHeap_.pop();

        maxHeap_.pop();

        
      
      
        return
      
      
         res;

    }

};




      
      
        int
      
      
         main() {

    freopen(
      
      
        "
      
      
        h.in
      
      
        "
      
      , 
      
        "
      
      
        r
      
      
        "
      
      
        , stdin);

    freopen(
      
      
        "
      
      
        h.out
      
      
        "
      
      , 
      
        "
      
      
        w
      
      
        "
      
      
        , stdout);

    
      
      
        int
      
      
         n;

    
      
      
        while
      
       ( scanf(
      
        "
      
      
        %d
      
      
        "
      
      , &
      
        n), n) {

        Solution s;

        
      
      
        int
      
       sum = 
      
        0
      
      
        ;

        
      
      
        for
      
       (
      
        int
      
       i = 
      
        0
      
      ; i < n; i++
      
        ) {

            
      
      
        int
      
      
         k;

            scanf(
      
      
        "
      
      
        %d
      
      
        "
      
      , &
      
        k);

            
      
      
        for
      
       (
      
        int
      
       j = 
      
        0
      
      ; j < k; j++
      
        ) {

                
      
      
        int
      
      
         t;

                scanf(
      
      
        "
      
      
        %d
      
      
        "
      
      , &
      
        t);

                s.push(t);

            }

            sum 
      
      +=
      
         s.getMaxMinDiff();

        }



        printf(
      
      
        "
      
      
        %d\n
      
      
        "
      
      
        , sum);

    }

}
      
    

?

Hoax or what


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 天天久久综合网站 | 午夜欧美精品 | 亚洲综合久久伊人热 | 991av| 漂流教室在线观看 | 99国产在线视频有精品视频 | 欧美精品第一页 | 日韩精品免费视频 | 亚州男人天堂 | 黄色视频a级毛片 | 色洛色中文综合网站 | 锵锵锵锵锵锵锵锵锵好大好湿软件 | 午夜精品影院 | 国产一卡2卡三卡4卡免费网站 | 久久久综合网 | 精品国产一区二区三区久久 | 国产精品69久久久久水密桃 | 亚洲97视频 | 99re视频在线观看 | 女猛烈无遮挡性视频免费 | 波多在线 | 色午夜在线| 午夜久久久久久网站 | 99在线精品免费视频九九视 | 狠狠躁日日躁夜夜躁A片小说按摩 | 久久久国产精品视频 | 国产精品亚洲综合第一区 | tube69欧美最新片 | 亚洲欧美精品一中文字幕 | 国产精品视频一区二区三区 | 国产精品久久福利新婚之夜 | 在线一区二区三区做爰视频网站 | 奇米视频在线 | 欧美国产日韩在线 | 特黄特色大片免费高清视频 | 日韩视频专区 | 国产高清在线精品 | 精品一区二区免费视频视频 | 精品福利在线视频 | 神马电影网午夜 | 亚洲iv一区二区三区 |