• <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>

            poj2010

            Moo University - Financial Aid

            Time Limit: 1000MS Memory Limit: 30000K
            Total Submissions: 3115 Accepted: 945

            Description

            Bessie noted that although humans have many universities they can attend, cows have none. To remedy this problem, she and her fellow cows formed a new university called The University of Wisconsin-Farmside,"Moo U" for short.

            Not wishing to admit dumber-than-average cows, the founders created an incredibly precise admission exam called the Cow Scholastic Aptitude Test (CSAT) that yields scores in the range 1..2,000,000,000.

            Moo U is very expensive to attend; not all calves can afford it.In fact, most calves need some sort of financial aid (0 <= aid <=100,000). The government does not provide scholarships to calves,so all the money must come from the university's limited fund (whose total money is F, 0 <= F <= 2,000,000,000).

            Worse still, Moo U only has classrooms for an odd number N (1 <= N <= 19,999) of the C (N <= C <= 100,000) calves who have applied.Bessie wants to admit exactly N calves in order to maximize educational opportunity. She still wants the median CSAT score of the admitted calves to be as high as possible.

            Recall that the median of a set of integers whose size is odd is the middle value when they are sorted. For example, the median of the set {3, 8, 9, 7, 5} is 7, as there are exactly two values above 7 and exactly two values below it.

            Given the score and required financial aid for each calf that applies, the total number of calves to accept, and the total amount of money Bessie has for financial aid, determine the maximum median score Bessie can obtain by carefully admitting an optimal set of calves.

            Input

            * Line 1: Three space-separated integers N, C, and F

            * Lines 2..C+1: Two space-separated integers per line. The first is the calf's CSAT score; the second integer is the required amount of financial aid the calf needs

            Output

            * Line 1: A single integer, the maximum median score that Bessie can achieve. If there is insufficient money to admit N calves,output -1.

            Sample Input

            3 5 70
            30 25
            50 21
            20 20
            5 18
            35 30
            

            Sample Output

            35
            

            Hint

            Sample output:If Bessie accepts the calves with CSAT scores of 5, 35, and 50, the median is 35. The total financial aid required is 18 + 30 + 21 = 69 <= 70.

            Source

            USACO 2004 March Green

            好題
            題目意思是
            告訴你要選出n個人(n為奇數)和總人數 和能提供的最大的幫助f
            再告訴你每個人的成績和所需的aid,
            然后我們要從其中找出n個人來,保證aid的和<=f的條件下,使得他們的中位數最大
            這題乍一看摸不著頭腦,我們可以來分析一下
            n為什么是奇數而不是偶數呢,顯然,奇數的話,中位數必然是一個固定的數,而不是兩個數的average
            這樣想,我們有一點思路了
            我們可以枚舉這個中位數,然后去驗證有沒有情況滿足
            但是怎么驗證呢
            首先,我們發現,有一部分必然不是中位數,這是前n/2小的和后n/2大的
            所以我們先排一下序,只去枚舉中間的一段當中位數,假設當前枚舉第i個
            那么我們必然要從左側選n/2個數,設其和為f1[i-1],從右側選n/2個數,設其和為f2[i+1]
            使得f1+f2+need[i]<=f,
            我們用f1[i-1]表示左側中選出n/2個最小的,f2[i+1] 表示……
            為什么和最小的呢,自己想去吧

            然后就是怎么選呢,
            這就用到最大堆的數據結構
            先考慮從左側選出n/2個使得和最小
            我們維護一個元素個數為n/2的最大堆,
            然后從n/2+1開始往堆中添加新元素,如果新元素小于堆頂,則添加并調整,
            這樣,我們總是能保證選出的元素和的值最小

            同樣右側選n/2個也是如此

            這樣,這道題就解決了

            最大堆維護最小和(dp)+枚舉


            苦逼的看題啊,最后沒有結果輸出-1,我輸出0,wa了6次
            哭……

            code
            #include <cstdio>
            #include 
            <cstdlib>
            #include 
            <cstring>
            #include 
            <cmath>
            #include 
            <ctime>
            #include 
            <cassert>
            #include 
            <iostream>
            #include 
            <sstream>
            #include 
            <fstream>
            #include 
            <map>
            #include 
            <set>
            #include 
            <vector>
            #include 
            <queue>
            #include 
            <algorithm>
            #include 
            <iomanip>
            #define maxn 200005
            using namespace std;
            struct node 
            {
                
            int score,need;
            }
            a[maxn],tmp1;
            long long sum,tmp;
            int nn;
            int n,c,f;
            int dp1[maxn],dp2[maxn];
            struct heapnode
            {
                node x[maxn];
                
            int num;
                
            void nii(int n)
                
            {
                    
            for(int i=1;i<=n/2;i++
                        swap(x[i],x[n
            -i+1]);
                }

                
            long long getsum()
                
            {
                    
            long long sum=0;
                    
            for(int i=1;i<=num;i++)
                    
            {
                        sum
            +=x[i].need;
                    }

                    
            return sum;
                }

                
            void down(int i,int m)
                
            {
                    
            int t=2*i;
                    
            while(t<=m)
                    
            {
                        
            if(t<m&&x[t].need<x[t+1].need) t++;
                        
            if(x[i].need<x[t].need)
                        
            {
                            swap(x[i],x[t]);
                            i
            =t;
                            t
            =i*2;
                        }

                        
            else break;
                    }

                }

                
            void change(node tmpx)
                
            {
                    x[
            1]=tmpx;
                    down(
            1,num);
                }

                
            void build()
                
            {
                    
            for(int i=num/2;i>=1;i--) down(i,num);
                }

            }
            heap1,heap2;
            bool cmp(node t1,node t2)
            {
                
            if(t1.score>t2.score) 
                    
            return 0;
                
            else if(t1.score<t2.score)
                    
            return 1;
                
            else return t1.need<t2.need;
            }

            /*void print(heapnode t1)
            {
                printf("\n");
                for(int i=1;i<=c;i++)
                    printf("%d %d\n",t1.x[i].score,t1.x[i].need);
                printf("\n");
            }
            */

            int main()
            {
                scanf(
            "%d%d%d",&n,&c,&f);
                
            for(int i=1;i<=c;i++) scanf("%d%d",&a[i].score,&a[i].need);
                sort(a
            +1,a+c+1,cmp);
                memcpy(heap1.x,a,
            sizeof(heap1.x));
                memcpy(heap2.x,a,
            sizeof(heap2.x));
                heap2.nii(c);
                nn
            =n/2;
                heap1.num
            =nn;
                heap2.num
            =nn;
                heap1.build();
                heap2.build();
                memset(dp1,
            0,sizeof(dp1));
                memset(dp2,
            0,sizeof(dp2));
                dp1[nn]
            =heap1.getsum();
                dp2[nn]
            =heap2.getsum();
                
            for(int i=nn+1;i<=c-nn;i++)
                
            {
                    
            if(heap1.x[i].need<heap1.x[1].need)
                    
            {
                        dp1[i]
            =dp1[i-1]-heap1.x[1].need+heap1.x[i].need;
                        heap1.change(heap1.x[i]);
                    }

                    
            else dp1[i]=dp1[i-1];
                }

                
            for(int i=nn+1;i<=c-nn;i++)
                
            {
                    
            if(heap2.x[i].need<heap2.x[1].need)
                    
            {
                        dp2[i]
            =dp2[i-1]-heap2.x[1].need+heap2.x[i].need;
                        heap2.change(heap2.x[i]);
                    }

                    
            else dp2[i]=dp2[i-1];
                }

                
            bool flag;
                flag
            =0;
                
            for(int i=c-nn;i>=nn+1;i--)
                
            {
                    
            if(a[i].need+dp1[i-1]+dp2[c-i]<=f)
                    
            {
                        printf(
            "%d\n",a[i].score);
                        flag
            =1;
                        
            break;
                    }

                }

                
            if(flag==0)
                    printf(
            "-1\n");
                
            return 0;
            }



            posted on 2012-07-25 09:09 jh818012 閱讀(225) 評論(0)  編輯 收藏 引用

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            常用鏈接

            留言簿

            文章檔案(85)

            搜索

            最新評論

            • 1.?re: poj1426
            • 我嚓,,輝哥,,居然搜到你的題解了
            • --season
            • 2.?re: poj3083
            • @王私江
              (8+i)&3 相當于是 取余3的意思 因為 3 的 二進制是 000011 和(8+i)
            • --游客
            • 3.?re: poj3414[未登錄]
            • @王私江
              0ms
            • --jh818012
            • 4.?re: poj3414
            • 200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。
            • --王私江
            • 5.?re: poj1426
            • 評論內容較長,點擊標題查看
            • --王私江
            久久国产亚洲高清观看| 精品乱码久久久久久夜夜嗨| 久久综合久久综合九色| 久久亚洲AV成人无码电影| 怡红院日本一道日本久久 | 亚洲国产精品一区二区久久| 亚洲狠狠婷婷综合久久久久| 久久午夜福利无码1000合集| 三级三级久久三级久久| 亚洲中文字幕无码久久2017| 亚洲精品乱码久久久久久按摩| 久久久久久精品免费看SSS| 亚洲午夜久久久久久噜噜噜| 国内精品人妻无码久久久影院导航 | 偷偷做久久久久网站| 久久久久久伊人高潮影院| 欧美亚洲色综久久精品国产| 99久久国产热无码精品免费| 久久99热狠狠色精品一区| 久久97久久97精品免视看秋霞| 国产免费久久精品99久久| 亚洲美日韩Av中文字幕无码久久久妻妇 | 韩国三级大全久久网站| 久久电影网| 久久国产色av免费看| 国产精品久久久久AV福利动漫| 99麻豆久久久国产精品免费| 久久精品中文字幕一区| 热99RE久久精品这里都是精品免费| 久久综合狠狠综合久久综合88| 欧美日韩中文字幕久久伊人| 麻豆精品久久久久久久99蜜桃| 97久久精品午夜一区二区| 狠狠精品干练久久久无码中文字幕| 久久亚洲精品国产精品婷婷 | 久久精品国产免费| 久久久久久久精品成人热色戒| 久久99热狠狠色精品一区| 影音先锋女人AV鲁色资源网久久| 热久久这里只有精品| 久久国产欧美日韩精品|