二分做法题解:P3963 [TJOI2013] 奖学金
题意
双倍经验 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;
}