EOJ:Moo University - Financial Aid

Moo University - Financial Aid

Time Limit: 1000MS Memory Limit: 65536K
Total Submits: 72 Accepted: 13

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.

 

地址:http://202.120.106.94/onlinejudge/problemshow.php?pro_id=129

——————————————————————————————————————————————————————————————

将所有的牛按分数排序

维护两个大根堆。一个放有比第I头牛分数低的且奖学金最小的N/2头牛,他们的奖学金总和为LEFT_SUM[I];一个放有比第I头牛分数高的且奖学金最小的N/2头牛,他们的奖学金总和为RIGHT_SUM[I]

如果AID[I]+为LEFT_SUM[I]+RIGHT_SUM[I]<=F则第I头牛可行。

找出分数最大的第I头牛即可。

另外LEFT_SUM和RIGHT_SUM可预先处理。

 

代码
1 #include<stdio.h>
2 #include<queue>
3 #include<stdlib.h>
4  using namespace std;
5  #define maxn 100005
6  struct node
7 {
8 int s,f;
9 }cow[maxn];
10  int right[maxn],left[maxn];
11  int i,ans,left_sum,right_sum,temp,n,c,f;
12 priority_queue<int,vector<int>,less<int> > q;
13  int cmp(const void *a,const void *b)
14 {
15 struct node *aa=(node *)a;
16 struct node *bb=(node *)b;
17 if ((aa->s)>(bb->s)) return 1;
18 return -1;
19 }
20 void work()
21 {
22 left_sum=0;
23 right_sum=0;
24 for (i=0;i<n/2;i++)
25 {
26 q.push(cow[i].f);
27 left_sum+=cow[i].f;
28 }
29 left[n/2]=left_sum;
30 for (i=n/2;i<c;i++)
31 {
32 temp=q.top();
33 if (temp>cow[i].f)
34 {
35 q.pop();
36 q.push(cow[i].f);
37 left_sum=left_sum-temp+cow[i].f;
38 }
39 left[i+1]=left_sum;
40 }
41 q=priority_queue<int,vector<int>,less<int> >(); //清空QUEUE
42 temp=q.size();
43 for (i=c-1;i>=c-n/2;i--)
44 {
45 q.push(cow[i].f);
46 right_sum+=cow[i].f;
47 }
48 right[c-n/2-1]=right_sum;
49 for (i=c-n/2-1;i>=0;i--)
50 {
51 temp=q.top();
52 if (temp>cow[i].f)
53 {
54 q.pop();
55 q.push(cow[i].f);
56 right_sum=right_sum-temp+cow[i].f;
57 }
58 if (i-1>=0)right[i-1]=right_sum;
59 }
60 ans=-1;
61 for (i=c-n/2-1;i>=n/2;i--)
62 if (cow[i].f+left[i]+right[i]<=f)
63 {
64 ans=cow[i].s;
65 break;
66 }
67 }
68 int main()
69 {
70 while (scanf("%d%d%d",&n,&c,&f)!=EOF)
71 {
72 for (i=0;i<c;i++)
73 scanf("%d%d",&cow[i].s,&cow[i].f);
74 qsort(cow,c,sizeof(cow[0]),cmp);
75 work();
76 printf("%d\n",ans);
77 }
78 return 0;
79 }

 

posted on 2011-02-04 20:48  风也轻云也淡  阅读(169)  评论(0编辑  收藏  举报