Moo University - Financial Aid
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 3708 | Accepted: 1099 |
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.
Source
USACO 2004 March Green
题目比较简单,二分答案即可。关键是要注意两个细节。1 n等于1时。2 注意相同的价值,要特别处理。
ac了后看discuss才发现这题不是那么随便的二分,因为是不满足单调性的,但竟然ac了,这。。。数据也太弱了吧。其实是需要根据cnt1和cnt2来判断二分的范围该往哪边缩小,代码就懒得改了。
题目比较简单,二分答案即可。关键是要注意两个细节。1 n等于1时。2 注意相同的价值,要特别处理。
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
using namespace std;
typedef long long LL;
const int maxn = 100000 + 5;
const int INF = 1000000000;
struct Node{
int value,cost;
bool operator < (const Node &a) const{
return a.cost < cost;
}
}a[maxn];
bool cmp(Node a,Node b){
return a.value < b.value;
}
priority_queue<Node> Q;
int n,c,f;
bool can(int x,int mid){
while(!Q.empty()) Q.pop();
for(int i = 0;i < c;i++){
if(i != mid)
Q.push(a[i]);
}
int cnt1 = 0,cnt2 = 0,cnt = 0;
int sum = f - a[mid].cost;
while(!Q.empty()){
Node tem = Q.top();Q.pop();
if(tem.value == x && sum >= tem.cost){
sum -= tem.cost;
cnt++;
}
else if(tem.value < x && cnt1 < n/2 && sum >= tem.cost){
sum -= tem.cost;
cnt1++;
}
else if(tem.value > x && cnt2 < n/2 && sum >= tem.cost){
sum -= tem.cost;
cnt2++;
}
if(cnt + cnt1 + cnt2 >= n-1){
return true;
}
}
return false;
}
int main(){
while(scanf("%d%d%d",&n,&c,&f) != EOF){
for(int i = 0;i < c;i++){
scanf("%d%d",&a[i].value,&a[i].cost);
}
if(n == 1){
if(a[0].cost <= f) printf("%d\n",a[0]);
else printf("-1\n");
continue;
}
sort(a,a+c,cmp);
int l = 0,r = c-1;
int ans = -1;
while(l <= r){
int mid = l+(r-l)/2;
if(can(a[mid].value,mid)){
ans = a[mid].value;
l = mid+1;
}
else{
r = mid-1;
}
}
printf("%d\n",ans);
}
return 0;
}
ac了后看discuss才发现这题不是那么随便的二分,因为是不满足单调性的,但竟然ac了,这。。。数据也太弱了吧。其实是需要根据cnt1和cnt2来判断二分的范围该往哪边缩小,代码就懒得改了。