• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 33,  comments - 33,  trackbacks - 0
            題目大意:
            對(duì)以下數(shù)組:
            struct Cow
            {
             int score;
             int aid;
            }cows[C];
            共C個(gè)cow,選出N個(gè)(N為奇數(shù)),使其aid的和在不大于給定的數(shù)F下,使這N個(gè)數(shù)的score的中位數(shù)最大。
            題解:依然使用堆,我們首先對(duì)牛的score進(jìn)行排序,然后我們從第N/2頭牛開始,到第C-N/2頭牛結(jié)束。
            每次假設(shè)第i頭牛就是中位數(shù)的牛,所以我們只需要計(jì)算這頭牛的前N/2和后N/2的aid部分的最小值側(cè)可。
            我們知道,在一個(gè)大數(shù)據(jù)中取固定數(shù)目最大或最小數(shù),用堆是最合理的。
            最大堆:選取數(shù)據(jù)中最小的數(shù)集合。
            最小堆:選取數(shù)據(jù)中最大的數(shù)的集合。
            我們使用兩個(gè)數(shù)組:
            before[i] : 表示第i頭牛前面的,選擇N/2頭牛,使其aid和最小的和的結(jié)果。
            當(dāng)i < N/2時(shí),為0
            after[i] : 表示第i頭牛后面的,選擇N/2頭牛,使其aid和最小的和的結(jié)果。
            當(dāng)i >= C - N/2時(shí),為0

            最后倒敘求,當(dāng)符合條件after[i]+cows[i].aid + before[i] <= f就打印退出

            代碼:
            #include <stdio.h>
            #include 
            <algorithm>
            using namespace std;

            const int C = 100005;
            const int N = 20000;
            int n,c,f;

            struct Cow 
            {
                
            int score;
                
            int aid;
                friend 
            bool operator < (const Cow& _a,const Cow &_b)
                
            {
                    
            return _a.score < _b.score;
                }

            }
            ;
            Cow cows[C];

            int before[C];
            int after[C];

            template
            <typename _Type>
            class MaxHeap
            {
            private:
                _Type data[N];
                
            int size;
                
            int cur;
            public:
                MaxHeap():size(
            0),cur(0){}
                MaxHeap(
            int _n):size(_n),cur(0)
                
            {
                    memset(data,
            0,sizeof(data));
                    data[
            0= 1 << 30;
                }

                
            ~MaxHeap()
                
            {

                }

                
            void clear(int _n)
                
            {
                    memset(data,
            0,sizeof(data));
                    cur 
            = 0;
                    data[
            0= 1 << 30;
                    size 
            = _n;
                }


                
            void push(_Type _value)
                
            {
                    
            if(isFull())
                    
            {
                        
            return ;
                    }

                    cur 
            ++;
                    
            int i;
                    
            for(i = cur; data[i/2< _value;i/=2)
                    
            {
                        data[i] 
            = data[i/2];
                    }

                    data[i] 
            = _value;
                }


                
            void pop()
                
            {
                    
            if(isEmpty())
                        
            return ;
                    
            int lastElement = data[cur];
                    data[cur] 
            = 0;
                    
            --cur;
                    
            int child = 0;
                    
            int i = 0;
                    
            for(i = 1; i*2 <= cur; i = child)
                    
            {
                        child 
            = i*2;
                        
            if(child != cur && data[child+1> data[child])
                        
            {
                            
            ++child;
                        }

                        
            if(lastElement < data[child])
                            data[i] 
            = data[child];
                        
            else
                            
            break;
                    }

                    data[i] 
            = lastElement;
                }


                
            int front()const
                
            {
                    
            return data[1];
                }


                
            bool isFull()const
                
            {
                    
            return cur >= size;
                }

                
            bool isEmpty()
                
            {
                    
            return cur == 0;
                }

            }
            ;


            MaxHeap
            <int> heap;


            void Test()
            {
                
            for (int i = 0; i < c; ++i)
                
            {
                    scanf(
            "%d %d",&(cows[i].score),&(cows[i].aid));
                }

                sort(cows,cows
            +c);
                
            int heapSize = n/2;
                heap.clear(heapSize);
                
            for (int i = 0; i < heapSize; ++i)
                
            {
                    heap.push(cows[i].aid);
                    before[heapSize] 
            += cows[i].aid;
                }

                
            for (int i = heapSize+1; i < c - heapSize; ++i)
                
            {
                    
            int fontV = heap.front();
                    
            if (fontV > cows[i-1].aid)
                    
            {
                        heap.pop();
                        heap.push(cows[i
            -1].aid);
                        before[i] 
            = before[i-1- fontV + cows[i-1].aid;
                    }

                    
            else
                    
            {
                        before[i] 
            = before[i-1];
                    }

                }


                heap.clear(heapSize);
                
            for (int i = c-1; i > c - 1 - heapSize; --i)
                
            {
                    heap.push(cows[i].aid);
                    after[c
            -1-heapSize] += cows[i].aid;
                }

                
            for (int i = c - 2 - heapSize; i >= heapSize; --i)
                
            {
                    
            int fontV = heap.front();
                    
            if (fontV > cows[i+1].aid)
                    
            {
                        heap.pop();
                        heap.push(cows[i
            +1].aid);
                        after[i] 
            = after[i+1- fontV + cows[i+1].aid;
                    }

                    
            else
                    
            {
                        after[i] 
            = after[i+1];
                    }

                }


                
            for (int i = c - 1 - heapSize; i >= heapSize; --i)
                
            {
                    
            if (after[i] + before[i] + cows[i].aid <= f)
                    
            {
                        printf(
            "%d\n",cows[i].score);
                        
            return;
                    }

                }

                printf(
            "-1\n");
            }


            int main()
            {
                
            //freopen("data.txt","r",stdin);
                while(scanf("%d %d %d",&n,&c,&f) != EOF)
                
            {
                    Test();
                }

                
            return 0;
            }

            posted on 2011-10-19 11:04 bennycen 閱讀(2477) 評(píng)論(1)  編輯 收藏 引用 所屬分類: 算法題解
            国产一级做a爰片久久毛片| 国内精品久久久久久99蜜桃| 国内精品久久久久伊人av| 少妇熟女久久综合网色欲| 国产午夜精品理论片久久| 久久福利青草精品资源站| 久久精品国产亚洲AV麻豆网站| 国内精品伊人久久久久777| 午夜精品久久久久久久无码| a级毛片无码兔费真人久久 | 久久精品嫩草影院| 久久久久久无码Av成人影院| 日韩精品久久无码中文字幕| 无遮挡粉嫩小泬久久久久久久| 久久精品aⅴ无码中文字字幕不卡 久久精品成人欧美大片 | 无码人妻久久一区二区三区免费丨| 亚洲国产成人乱码精品女人久久久不卡| 精品免费久久久久国产一区 | 久久国产成人精品国产成人亚洲| 精品综合久久久久久97超人| 久久香蕉国产线看观看99| 94久久国产乱子伦精品免费| 国产精品免费看久久久香蕉| 免费一级欧美大片久久网| 少妇熟女久久综合网色欲| 人妻少妇久久中文字幕| 久久99国内精品自在现线| 色综合久久天天综合| 久久国产影院| 亚洲人成网亚洲欧洲无码久久| 日韩人妻无码精品久久免费一| 久久精品国产精品青草| 久久男人中文字幕资源站| 久久久久久精品久久久久| www久久久天天com| www亚洲欲色成人久久精品| 日韩久久无码免费毛片软件 | 69久久夜色精品国产69| 久久97久久97精品免视看秋霞| 久久久久免费精品国产| 99久久婷婷免费国产综合精品|