Moo University - Financial Aid POJ - 2010

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. 

奶牛大学招生,现在有C个奶牛,招生N个奶牛,每个奶牛有两个参数,一个是分数,一个是所需要的资助。现在要求满足所招的奶牛要求的资助不超过F,且中位数最大,如果不满足条件,那就输出-1.

这个题就是先预处理中位数左右的最小的所需的资助,从大到小枚举中位数,

左右所需的最小资助,加上当前中位数所对应的资助。只要满足条件,那就是最优解,然后退出程序。如果找不到,那就-1.


#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+10;
struct node{
    int a,b;
}p[MAXN];
long long l[MAXN],r[MAXN];
int N,F,C;
bool cmp(node const &a,node const &b){
    return a.a < b.a;
}
int main()
{
    int m;
    scanf("%d%d%d",&N,&C,&F);
    for (int i = 0; i < C; i++){
        scanf("%d%d",&p[i].a,&p[i].b);
    }
    sort(p,p+C,cmp);
    m = N / 2;
    priority_queue<int> P; //优先队列,从大到小排序。
    long long sum = 0;
    for (int i = 0; i < m; i++){
        P.push(p[i].b);
        sum += p[i].b;
    }
    l[m-1] = sum;         //这个是预处理,处理当前是中位数,之前所有的数中最小的资助是多少。
    for (int i = m; i < C-m; i++){
        if (p[i].b < P.top())
        {
            sum -=P.top();
            P.pop();
            P.push(p[i].b);
            sum += p[i].b;
            l[i] = sum;
        } else l[i] = l[i-1];
    }


    while(!P.empty()) P.pop();
    sum = 0; //  //这个是预处理,处理当前是中位数,之后所有的数中最小的资助是多少。
    for (int i = C-1; i >= C-m; i--){
        P.push(p[i].b);
        sum += p[i].b;
    }
    r[C-m] = sum;
    for (int i = C-m-1; i>=m; i--){
        if (p[i].b<P.top()){
            sum -= P.top();
            P.pop();
            P.push(p[i].b);
            sum += p[i].b;
            r[i] = sum;
        } else r[i] = r[i+1];
    }
    for (int i = C-m-1; i >= m; i--)  //左右的预处理和当前的中位数对应的资助加起来,满足条件就是最优解。
    {
        if (r[i+1]+l[i-1]+p[i].b <= F)
        {
            printf("%d\n",p[i].a);
            return 0;
        }
    }
    printf("-1\n");
    return 0;
}





猜你喜欢

转载自blog.csdn.net/kidsummer/article/details/80257989