poj 2010 Moo University

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

 

题意:奶牛学校招生,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
",cow[i].sco);
78                 flag=1;
79                 break;
80             }
81         }
82         if(flag==0){
83             printf("-1
");
84         }
85     }
86     return 0;
87 }
View Code
原文地址:https://www.cnblogs.com/UniqueColor/p/4771541.html