POJ-2010-优先队列(解题报告)

Moo University - Financial Aid
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4701 Accepted: 1427

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(奇数)个学生提供助学金,但是该学校有点穷,最多能提供助学金数额为F。现在总共有C个学生可待选择,给出了这些学生的成绩以及相应的助学金,然而学校希望这个N个学生的成绩的中位数尽可能地大,求这个中位数的最大值。

输入:第一行, N(1 <= N <= 19,999),C(N <= C <= 100,000),  F( 0 <= F <= 2,000,000,000);
            2...C+1行,每行两个数,score(1<=score<=2000000000),  aid(0 <= aid <=100,000)。
输出:可能的最大的中位数(成绩)。如果资金不足以承受这些学生则输出 -1。



解题思路:

                 对于该题,我们可以枚举中位数来确定,即从第 n/2+1~C-n/2个学生一个一个枚举出以其中的学生作为中位数所能确定的助学金的最小值,即让左边的n/2个学生与右边的n/2个学生的助学金尽可能小。怎么确定呢?


实现:

记录牛学生的数组
> struct Cow{
>     int score,aid;
>     int left,right;
> }c[100001];
> priority_queue<int>q;
> 先以score按升序排序,(假设c数组1开始从左到右)
> 1、把最左边的n/2个学生的aid值放到队列里去,从n/2+1开始到C-n/2的学生中,依次替换队列中最大的数,并且更新它们的总和,这个总和,记录在相应的  c的left中,表示  比这个学生低分的n/2个学生的助学金最小和。

> 2,同理可以求出比这个学生高分的n/2个学生的助学金最小和,存入相应的c的right中。

> 3,从C-n/2个学生开始找,只要找到满足c[i].left+c[i].aid+c[i].right<=F, 就可以输出该学生的score,跳出循环。



代码:

#include<queue>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;

const int max_n=100000+5;
struct P
{
    int x;
    int y;
    int left;
    int right;
}e[max_n];

bool cmp(P a,P b)
{
    return a.x<b.x;
}

int main()
{
    int N,C,F;
    memset(e,0,sizeof(e));
    scanf("%d%d%d",&N,&C,&F);
    for(int i=1;i<=C;i++)
        scanf("%d%d",&e[i].x,&e[i].y);
    sort(e+1,e+1+C,cmp);
    if(N>1){
        priority_queue<int>q;
        int p,t,sum=0;
        for(int i=1;i<=N/2;i++)
        {
            p=e[i].y;
            q.push(p);
            sum+=p;
        }
        for(int i=N/2+1;i<=C-N/2;i++)
        {
            e[i].left=sum;            //比这个学生低分的n/2个学生的助学金最小和。
            p=q.top();
            t=e[i].y;
            if(t<p){
                q.pop();
                q.push(t);
                sum=sum-p+t;
            }
        }
        while(q.size()) q.pop();
        sum=0;
        for(int i=C;i>=C-N/2+1;i--)
        {
            p=e[i].y;
            q.push(p);
            sum+=p;
        }
        for(int i=C-N/2;i>=N/2+1;i--)
        {
            e[i].right=sum;       //比这个学生高分的n/2个学生的助学金最小和。
            p=q.top();
            t=e[i].y;
            if(t<p){
                q.pop();
                q.push(t);
                sum=sum-p+t;
            }
        }
    }
    int ans=-1;
    for(int i=C-N/2;i>=N/2+1;i--)
    {
        int total=e[i].left+e[i].right+e[i].y;
        if(total<=F)
        {
            ans=e[i].x;
            break;
        }
    }
    cout<<ans<<endl;
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值