標籤:
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 7030 2550 2120 205 1835 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
題意:奶牛學校招生,c頭奶牛報名,要選n頭(n為奇數),學校是義務制,所以每頭奶牛的學費都由學校負責。每頭奶牛都由自己的考試分數和它需要花的學費,學校總共有f的資金,問合法招生方案中中間分數(即排名第(n+1)/2)最高的是多少。
題解:先將所有的奶牛按照分數由高到低排序,假設k是招的奶牛中排名中間的那頭,按照排序可知,[1,k-1]中的奶牛必定被招了(n-1)/2頭,[k+1,c]中也必定被招了(n-1)/2頭,而且無論招的是誰,分數是怎麼樣,最後影響結果的都只是k的分數。於是,可以預先處理dpl[i]代表[1,i]頭牛中選出(n-1)/2頭牛的最小花費,dpr[i]代表[i,c]頭牛中選出(n-1)/2頭牛的花費,預先處理方法可以用一個大頂堆,複雜度nlogn,最後枚舉中間牛複雜度n。
步驟:1、先按牛的分數從大到小排序
2、預先處理dpL、dpR數組,dpL表示 1-?的最低花費,dpR表示 ?-c 的最低花費,這裡用了優先隊列來實現
3、從高到低枚舉能作為中位元的牛,看看能否符合,符合直接break
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 #define N 101006 8 #define ll long long 9 ll n,c,f;10 struct Node{11 ll sco;12 ll cost;13 }cow[N];14 bool cmp(Node a,Node b){15 if(a.sco!=b.sco)16 return a.sco>b.sco;17 return a.cost<b.cost;18 }19 20 ll dpL[N],dpR[N];21 22 int main()23 {24 while(scanf("%I64d%I64d%I64d",&n,&c,&f)==3){25 for(int i=0;i<c;i++){26 scanf("%I64d%I64d",&cow[i].sco,&cow[i].cost);27 }28 sort(cow,cow+c,cmp);29 memset(dpL,0,sizeof(dpL));30 memset(dpR,0,sizeof(dpR));31 32 33 priority_queue<int>q;34 ll niu=(n-1)/2;35 ll sum=0;36 for(int i=0;i<niu;i++){37 q.push(cow[i].cost);38 sum+=cow[i].cost;39 }40 dpL[niu-1]=sum;41 for(ll i=niu;i<c;i++){42 if(q.top()>cow[i].cost){43 sum=sum-q.top()+cow[i].cost;44 q.pop();45 q.push(cow[i].cost);46 dpL[i]=sum;47 }48 else{49 dpL[i]=dpL[i-1];50 }51 }52 53 while(!q.empty()){54 q.pop();55 }56 57 sum=0;58 for(ll i=c-1;i>=c-niu;i--){59 q.push(cow[i].cost);60 sum+=cow[i].cost;61 }62 dpR[c-niu]=sum;63 for(ll i=c-niu-1;i>=0;i--){64 if(q.top()>cow[i].cost){65 sum=sum-q.top()+cow[i].cost;66 q.pop();67 q.push(cow[i].cost);68 dpR[i]=sum;69 }70 else{71 dpR[i]=dpR[i+1];72 }73 }74 ll flag=0;75 for(int i=niu;i<c-niu;i++){76 if(cow[i].cost+dpL[i-1]+dpR[i+1]<=f){77 printf("%I64d\n",cow[i].sco);78 flag=1;79 break;80 }81 }82 if(flag==0){83 printf("-1\n");84 }85 }86 return 0;87 }View Code
poj 2010 Moo University - Financial Aid(優先隊列(最小堆)+ 貪心 + 枚舉)