水题
但是处理方式值得学习。
题意
有
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;
}