贪心+优先队列(需优化) POJ2010

Moo University - Financial Aid

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.

这道题目有两种思路,第一种可以用优先队列,第二种是二分法。
下面主要讲一下优先队列的做法,因为如果不优化,那么就会超时,可坑了;一般思路就是枚举中位数,先按照分数排序(两端的(N-1)/2个数是不能取的,理由是在保证中位数最大的情况下,保证正好取N个!根据 sorce递增排序, 枚举中位数a[i]! 那么肯定在 [1,i-1]区间内取N/2个数, 在 [i+1,C]内取N/2个数!对吧);每次枚举是都要去两边搜索,这一来,就很浪费时间了,所以我们要优化;
具体的优化思路就是先预处理,保存我枚举中位数的时候左边和右边的最小aid和,这样子最后只要从最大的可取的中位数开始枚举,判断leftsum[i-1]+p[i].aid+rightsum[i+1]<=F是否满足,大大缩小了时间。
代码如下:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define INF 0x3f3f3f3f
using namespace std;
//E - Moo University - Financial Aid
int N,C,F;
int lsum[100050],rsum[100050],temp=0;
typedef struct stu{
    int sco,aid;
    bool operator < (const stu &a) const  //定义堆的优先级 为大根堆
        {
            return aid<a.aid;
        }
}stu;
stu p[100010];
bool cmp(stu a,stu b){
    //if(a.aid==b.aid)
    return a.sco<b.sco;
    //else return a.aid<b.aid;
}
int main(){
    memset(lsum,0,sizeof(lsum));
    memset(rsum,0,sizeof(rsum));//初始化也很重要
    cin>>N>>C>>F;
    for(int i=1;i<=C;i++)//从1开始就可以了,宿sum[0]有用
        cin>>p[i].sco>>p[i].aid;
    sort(p+1,p+C+1,cmp);
    priority_queue<stu> q1,q2;//分别保存左右的aid
    int ans=-1;//没有答案就输出-1
    int i;
    for(i=1;i<=C;i++){
        if(i<=(N-1)/2){
            q1.push(p[i]);
            lsum[i]+=p[i].aid+lsum[i-1];
            continue;
        }
        else {
            if(p[i].aid<q1.top().aid){
                lsum[i]-=q1.top().aid;
                q1.pop();
                lsum[i]+=p[i].aid+lsum[i-1];
                q1.push(p[i]);
            }
            else lsum[i]=lsum[i-1];
        }
    }
    //temp=0;
    for(i=C;i>=1;i--){
        if(i>=C-(N-1)/2+1){
            q2.push(p[i]);
            rsum[i]+=p[i].aid+rsum[i+1];
            continue;
        }
        else {
            if(p[i].aid<q2.top().aid){//p[i].aid满足小于队列中最大的aid就更新队列数据
                rsum[i]-=q2.top().aid;
                q2.pop();
                rsum[i]+=p[i].aid+rsum[i+1];
                q2.push(p[i]);
            }
            else rsum[i]=rsum[i+1];//注意数据的继承
        }
    }
    for(i=C-(N-1)/2;i>=(N-1)/2+1;i--){
        if(lsum[i-1]+rsum[i+1]+p[i].aid<=F){
            ans=p[i].sco;
            break;//满足就直接输出了
        }
    }cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值