Description
Bessie noted that although humans has many universities they can attend, cows has none. To remedy ThisProblem, she and her fellow cows formed aNewUniversity 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 (CSA T) that yields scoresinchThe range1..2, the, the, the. Moo U isVery expensive to attend; Not all calves can afford it. In fact, the most calves need some sort of financial aid (0<= Aid <= -, the). The government does not provide scholarships to calves,so all the money must come fromThe University'S Limited fund (whose total money is F, 0 <= F <= 2,000,000,000).Worse Still, Moo U only have classrooms forAn odd number N (1<= N <= +,999) of the C (N <= c <= -, the) calves who has applied. Bessie wants to admit exactly N calvesinchOrder to maximize educational opportunity. She still wants the median CSAT score of the admitted calves to be asHigh aspossible. Recall that the median of aSetof integers whose size isOdd isThe middle value when they is sorted. For example, the median of theSet{3,8,9,7,5} is 7, asThere is exactly the values above7And exactly the values below it. Given the score and required financial aid forEach calf this applies, the total number of calves to accept, and the total amount of money Bessie have forFinancial aid, determine the maximum median score Bessie can obtain by carefully admitting an optimalSetof calves.
Input
1: three space-2.. c+1 is the calf'
Output
1 is insufficient money to admit N Calves,output-1
Sample Input
3 5 - - - - + - - 5 - * -
Sample Output
35
Hint
Sample Output:If Bessie accepts the calves with CSAT scores of 5, 35, and, the median is. The total financial aid required is + 70 + + = <=.
Source
Usaco 2004 March Green
Test instructions: Dairy School Admissions, C Cow registration, to choose N Head (n is odd), the school is compulsory, so each cow's tuition is responsible for the school. Each cow is made up of its own test scores and the tuition it needs to spend, and the school has a total of F's funding, asking how much of the median (i.e. rank (n+1)/2) is highest in the legal admissions program.
The puzzle: First of all the cows in accordance with the score from high to low, assuming that K is the top ranked in the middle of the cow, according to the sort, [1,k-1] cows must be recruited (n-1)/2 head, [K+1,C] also must be recruited (n-1)/2 head, and no matter who is the recruit, the score is how, The result of the final effect is only the K score. Thus, can be preprocessed dpl[i] represents [1,i] the minimum cost of a cow (n-1)/2 cow, dpr[i] represents [I,c] the cost of selecting (n-1)/2 cows in a cow, the preprocessing method can be used with a large top heap, the complexity Nlogn, and the last enumeration of the intermediate cow complexity N.
Step: 1, first by the cattle score from the big to the small sort
2, preprocessing DPL, DPR Array, DPL represents 1-? The minimum cost, DPR said? The minimum cost of-C, where priority queues are used to implement
3, from high to low enumeration can be used as the median of the cow, see if it can meet, in line with the direct break
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <queue>6 using namespacestd;7 #defineN 1010068 #definell Long Long9 ll N,c,f;Ten structnode{ One ll SCO; A ll cost; - }cow[n]; - BOOLCMP (Node A,node b) { the if(a.sco!=B.sco) - returnA.sco>B.sco; - returna.cost<B.cost; - } + - ll Dpl[n],dpr[n]; + A intMain () at { - while(SCANF ("%i64d%i64d%i64d", &n,&c,&f) = =3){ - for(intI=0; i<c;i++){ -scanf"%i64d%i64d",&cow[i].sco,&cow[i].cost); - } -Sort (cow,cow+c,cmp); inmemset (DpL,0,sizeof(DpL)); -memset (DpR,0,sizeof(DpR)); to + -priority_queue<int>Q; thell niu= (n1)/2; *ll sum=0; $ for(intI=0; i<niu;i++){Panax Notoginseng Q.push (cow[i].cost); -sum+=Cow[i].cost; the } +dpl[niu-1]=sum; A for(LL i=niu;i<c;i++){ the if(Q.top () >cow[i].cost) { +Sum=sum-q.top () +Cow[i].cost; - Q.pop (); $ Q.push (cow[i].cost); $dpl[i]=sum; - } - Else{ thedpl[i]=dpl[i-1]; - }Wuyi } the - while(!Q.empty ()) { Wu Q.pop (); - } About $sum=0; - for(LL i=c-1; i>=c-niu;i--){ - Q.push (cow[i].cost); -sum+=Cow[i].cost; A } +dpr[c-niu]=sum; the for(LL i=c-niu-1; i>=0; i--){ - if(Q.top () >cow[i].cost) { $Sum=sum-q.top () +Cow[i].cost; the Q.pop (); the Q.push (cow[i].cost); thedpr[i]=sum; the } - Else{ indpr[i]=dpr[i+1]; the } the } Aboutll flag=0; the for(inti=niu;i<c-niu;i++){ the if(cow[i].cost+dpl[i-1]+dpr[i+1]<=f) { theprintf"%i64d\n", Cow[i].sco); +flag=1; - Break; the }Bayi } the if(flag==0){ theprintf"-1\n"); - } - } the return 0; the}
View Code
POJ Moo university-financial Aid (priority queue (minimum heap) + Greedy + enumeration)