Moo University - Financial Aid
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 4109 | Accepted: 1257 |
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
-----------------------------------------------------------------
数据比较大,要起码O(nlogn)才能不超时。
将cow按分数排序,然后从高到低遍历中位数分数,若满足总需求小于等于F则输出,结束。
数据结构:优先队列(最大堆)
在插入数据的同时计算前i个cow中小于中位数的前N/2的最小需求值low[i]以及前j个cow大于中位数的后N/2的最小需求值high[j]
若中位数需求值cow.need[i] + low[i-1] + high[i+1] <= F 则满足题意,因为是按成绩的降序遍历,所以一旦找到立刻输出。
注意:当N == 1 时的情况要特殊处理。
教训:因为将从0开始和1开始的数组下标处理的不好,调试了好久,后来改成从1开始但因为有一个地方没有改过来,又WA了一次。。。真蛋疼。。。。下次要特别留意这些细节。。
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
#define MAX 100010
int N, C, F, low[MAX], high[MAX];
struct Cow
{
int score, need;
bool operator<(const Cow x) { return score < x.score;}
}cow[MAX];
priority_queue<int> que, que1;
int main()
{
//freopen("in.txt","r",stdin);
scanf("%d%d%d", &N, &C, &F);
for(int i = 1; i <= C; i++)
scanf("%d%d", &cow[i].score, &cow[i].need);
sort(cow+1, cow+1+C);
if(N == 1)
{
int ans = -1;
for(int i = C; i > 0; i--)
if(cow[i].need <= F)
ans = cow[i].score;
printf("%d\n", ans);
return 0;
}
int n = N/2, sum = 0;
for(int i = 1; i <= n; i++)
{
que.push(cow[i].need);
sum += cow[i].need;
}
low[n] = sum;
for(int i = n+1; i <= C-n; i++)
{
low[i] = low[i-1];
if(cow[i].need < que.top())
{
low[i] -= que.top() - cow[i].need;
que.pop();
que.push(cow[i].need);
}
}
sum = 0;
for(int i = C; i > C-n; i--)
{
que1.push(cow[i].need);
sum += cow[i].need;
}
high[C-n+1] = sum;
for(int i = C-n; i > n; i--)
{
high[i] = high[i+1];
if(cow[i].need < que1.top())
{
high[i] -= que1.top() - cow[i].need;
que1.pop();
que1.push(cow[i].need);
}
}
int i, ans = -1;
for(i = C-n; i > n; i--)
if(low[i-1] + high[i+1] + cow[i].need <= F)
{
ans = cow[i].score; break;
}
printf("%d\n", ans);
return 0;
}