[TJOI2013] 奖学金
代码:
#include<bits/stdc++.h>
using namespace std;
long long n,c,ff,ans;
long long suma[200010],sumb[200010];//前 后
struct score{ long long a,b; }s[200010];
bool cmp(score aa,score bb){return aa.a<bb.a; }
priority_queue<int>q;
int main()
{
scanf("%lld%lld%lld",&n,&c,&ff);
for(int i=1;i<=c;i++)
{
scanf("%lld%lld",&s[i].a,&s[i].b);
}
sort(s+1,s+c+1,cmp);
for(int i=1;i<=n/2;i++)
{
q.push(s[i].b);
suma[i]=suma[i-1]+s[i].b;
}
for(int i=n/2+1;i<=c;i++)
{
if(s[i].b<q.top())
{
suma[i]=suma[i-1]-q.top()+s[i].b;
q.pop();
q.push(s[i].b);
}
else suma[i]=suma[i-1];
}
while(q.size()) q.pop();
for(int i=c;i>=c-n/2+1;i--)
{
q.push(s[i].b);
sumb[i]=sumb[i+1]+s[i].b;
}
for(int i=c-n/2;i>=1;i--)
{
if(s[i].b<q.top())
{
sumb[i]=sumb[i+1]-q.top()+s[i].b;
q.pop();
q.push(s[i].b);
}
else sumb[i]=sumb[i+1];
}
for(int i=n/2+1;i<=c-n/2;i++)
if(suma[i-1]+sumb[i+1]+s[i].b<=ff)
ans=s[i].a;
if(ans==0)
{
printf("-1");
return 0;
}
printf("%lld",ans);
return 0;
}
整体思路在于从中位数入手,枚举每个数作为中位数,分别算出两边奖学金
用堆(优先队列)维护两边每次最小奖学金总和,时间复杂度O(nlongn)