题意
双倍经验 P4952。
你需要从 个学生中选 个给奖学金,每个学生有分数和需要的奖学金,总资金 。
要求选的这 个分数的中位数最大,求这个最大值。
分析
首先按照每个学生需要的奖学金升序排序。
显然答案有可二分性,对答案二分,check 函数就直接扫过去,找到需求前 个小的分数大于等于二分值的学生,直接加到答案里,再选剩余的需求最小的学生即可。具体实现参见代码。
复杂度 , 为最大分数。其实可以通过二分学生的中位数来变成 ,但其实差不多。
实现
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
struct node
{
LL a,b;
bool operator < (const node &w) const
{
return b < w.b;
}
}a[N];
LL k,n,m;
bool check(LL x)
{
LL res1 = 0,res2 = 0,cnt1 = 0,cnt2 = 0;
for(int i = 1; i <= n; i ++)
{
if(a[i].a >= x && cnt1 < (k + 1) / 2) res1 += a[i].b,cnt1 ++;
else if(cnt2 < k - (k + 1) / 2) res2 += a[i].b,cnt2 ++;
}
if(cnt1 < (k + 1) / 2) return 0;
return res1 + res2 <= m;
}
int main()
{
cin>>k>>n>>m;
for(int i = 1; i <= n; i ++) cin>>a[i].a>>a[i].b;
sort(a + 1,a + n + 1);
LL l = -1,r = 2e9;
while(l < r)
{
LL mid = l + r + 1 >> 1;
if(check(mid)) l = mid;
else r = mid - 1;
}
cout<<l<<endl;
return 0;
}