Moo University - Financial Aid
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 4522 | Accepted: 1370 |
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.
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
* 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.
题意:找到一个最大的……(根据样例我就说是左数和右数方便点吧),找到一个最大的左数,使得存在N/2的比这个左数小的和N/2的比这个左数大的这些对数右数的和小于等于F。
思路:以左数为关键字排序后二分这个最大的左数,然后再将其以右数为关键字排序,找到符合要求的数。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
struct node
{ int a,b;
}cow[200010];
node cow2[100010];
int n,c;
long long f;
bool cmp1(node a,node b)
{ if(a.a<b.a)
return true;
else
if(a.a==b.a)
return a.b<b.b;
else
return false;
}
bool cmp2(node a,node b)
{ return a.b<b.b;
}
bool solve(int x)
{ int i,k,value=cow[x].a,num1=0,num2=0,num3=0;
long long sum=f-cow[x].b;
for(i=1;i<=c;i++)
{ cow2[i].a=cow[i].a;
cow2[i].b=cow[i].b;
}
cow2[x].a=cow2[c].a;
cow2[x].b=cow2[c].b;
sort(cow2+1,cow2+c,cmp2);
for(i=1;i<c;i++)
{ if(cow2[i].a==value)
{ sum-=cow2[i].b;
num3++;
}
else if(cow2[i].a<value && num1<n/2 && sum>=cow2[i].b)
{ sum-=cow2[i].b;
num1++;
}
else if(cow2[i].a>value && num2<n/2 && sum>=cow2[i].b)
{ sum-=cow2[i].b;
num2++;
}
if(num1+num2+num3>=n-1)
return true;
}
return false;
}
int main()
{ int i,j,ans=-1;
scanf("%d%d%I64d",&n,&c,&f);
for(i=1;i<=c;i++)
scanf("%d%d",&cow[i].a,&cow[i].b);
sort(cow+1,cow+1+c,cmp1);
int l=1,r=c,mi;
while(l<=r)
{ mi=(l+r)/2;
if(solve(mi))
{ ans=cow[mi].a;
l=mi+1;
}
else
r=mi-1;
}
if(ans!=-1)
printf("%d\n",ans);
else
printf("-1\n");
}