P3963 [TJOI2013]奖学金

水题
但是处理方式值得学习。

题意

c c c个同学,有两个属性,成绩和奖学金。
选出 n n n个人,要求奖学金总额不超过 f f f,且中位数尽可能大。

题解

首先呢,中位数是个很难处理的东西,所以如果我们先排序,直接枚举中间那些中位数即可。
但是这个时候考虑到奖学金,还需要预处理中位数左右侧 n / 2 n/2 n/2小的数的和。用于判断会不会超过 f f f

处理方式是用优先队列存好 n / 2 n/2 n/2个,然后逐一比较,一旦比最大的小,就可以替换进来。

#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define RFOR(i,a,b) for(int i=a;i>=b;i--)
using namespace std;

typedef long long ll;
const int maxn = 3e5+200;

priority_queue<int>q;

int n,c;ll f,tmp;

struct node{
    int val,cost;
    friend bool operator < (node a,node b){
        return a.val<b.val;
    }
}A[maxn];

ll p1[maxn],p2[maxn];

int main(){
    cin>>n>>c>>f;
    FOR(i,1,c)scanf("%d%d",&A[i].val,&A[i].cost);
    tmp=0;
    sort(A+1,A+1+c);
    FOR(i,1,n/2){
        q.push(A[i].cost);
        tmp+=1ll*A[i].cost;
    }
    FOR(i,n/2+1,c-n/2){
        p1[i]=tmp;
        if(q.top()>A[i].cost){
            tmp-=1ll*q.top();q.pop();
            q.push(A[i].cost);tmp+=1ll*A[i].cost;
        }
    }
    while(!q.empty())q.pop();
    tmp=0;
    RFOR(i,c,c-n/2+1){
        q.push(A[i].cost);
        tmp+=1ll*A[i].cost;
    }
    RFOR(i,c-n/2,n/2+1){
        p2[i]=tmp;
        if(q.top()>A[i].cost){
            tmp-=1ll*q.top();q.pop();
            q.push(A[i].cost);tmp+=1ll*A[i].cost;
        }
    }
    int ans=-1;
    FOR(i,n/2+1,c-n/2){
        if(p1[i]+p2[i]+1ll*A[i].cost<=f){
            ans=max(ans,A[i].val);
        }
    }
    cout<<ans<<endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值