Moo University - Financial Aid
Time Limit: 1000MS
Memory Limit: 30000K
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-1)/2个数字,那么假设下标从0开始,可取的中位数的起点就是(N-1)/2,终点就是C-1-(N-1)。然后题目中要求最大的中位数,但是也要满足总学费不能超过F。那么我们就还要得知每个中位数的两边的其他(N-1)个数字的费用的最小和,这里就可以用到DP和优先队列。这里中位数左边数字的最小和与右边的要分开求。假设dpl[i]的意义是下标为i的数字的左边其他数字中选(N-1)/2个数字的最小和,dpr[i]就相应代表右边的。先初始化:对于下标为(N-1)/2的数字,也就是最小的中位数,dpl显然是确定的了,这里求和的同时把每个元素的费用都压入一个最大堆(从大到小的优先队列)。然后对于后面的下标为i的中位数而言,只要下标为i-1的元素的费用比最大堆中的顶端元素小,也就是存在一个更小的情况,那么dpl[i]=dpl[i-1]-(顶端元素)+(下标为i-1元素的费用),否则,dpl[i]=dpl[i-1]。那么同理右边的dpr也是相似的求法。最后从大到小遍历中位数,判断中位数加两端的费用是否不超过F,只要遇到符合要求的立马break,打出答案,没找到那自然就是-1了。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
int N,C,F,lr,sum,flag,ans;
int dpl[100010],dpr[100010];
struct k
{
int goal,fee;
}cow[100010];
priority_queue< int > q;
bool cmp(k a,k b)
{
if(a.goal!=b.goal)
return a.goal<b.goal;
return a.fee<b.fee;
}
int main()
{
while(cin>>N>>C>>F)
{
while(!q.empty())
q.pop();
lr=(N-1)/2,sum=0,flag=0;
for(int i=0;i<C;++i)
scanf("%d%d",&cow[i].goal,&cow[i].fee);
sort(cow,cow+C,cmp);
for(int i=0;i<lr;++i)
{
q.push(cow[i].fee);
sum+=cow[i].fee;
}
dpl[lr]=sum;
for(int i=lr+1;i<=C-1;++i)
{
if(!q.empty()&&cow[i-1].fee<q.top())
{
dpl[i]=dpl[i-1]-q.top()+cow[i-1].fee;
q.pop();
q.push(cow[i-1].fee);
}
else
dpl[i]=dpl[i-1];
}
while(!q.empty())
q.pop();
sum=0;
for(int i=C-1;i>C-1-lr;--i)
{
q.push(cow[i].fee);
sum+=cow[i].fee;
}
dpr[C-1-lr]=sum;
for(int i=C-1-lr-1;i>=0;--i)
{
if(!q.empty()&&cow[i+1].fee<q.top())
{
dpr[i]=dpr[i+1]-q.top()+cow[i+1].fee;
q.pop();
q.push(cow[i+1].fee);
}
else
dpr[i]=dpr[i+1];
}
for(int i=C-1-lr;i>=lr;--i)
{
if(cow[i].fee+dpl[i]+dpr[i]<=F)
{
flag=1;
ans=cow[i].goal;
break;
}
}
if(flag)
printf("%d\n",ans);
else
printf("-1\n");
}
}