好题分析---2021.11.10

Moo University - Financial Aid

 

 Bessie noted that although humans have many universities they can attend, cows have none. To remedy this problem, she and her fellow cows formed a new university called The University of Wisconsin-Farmside,"Moo U" for short.

Not wishing to admit dumber-than-average cows, the founders created an incredibly precise admission exam called the Cow Scholastic Aptitude Test (CSAT) that yields scores in the range 1..2,000,000,000.

Moo U is very expensive to attend; not all calves can afford it.In fact, most calves need some sort of financial aid (0 <= aid <=100,000). The government does not provide scholarships to calves,so all the money must come from the university's limited fund (whose total money is F, 0 <= F <= 2,000,000,000).

Worse still, Moo U only has classrooms for an odd number N (1 <= N <= 19,999) of the C (N <= C <= 100,000) calves who have applied.Bessie wants to admit exactly N calves in order to maximize educational opportunity. She still wants the median CSAT score of the admitted calves to be as high as possible.

Recall that the median of a set of integers whose size is odd is the middle value when they are sorted. For example, the median of the set {3, 8, 9, 7, 5} is 7, as there are exactly two values above 7 and exactly two values below it.

Given the score and required financial aid for each calf that applies, the total number of calves to accept, and the total amount of money Bessie has for financial aid, determine the maximum median score Bessie can obtain by carefully admitting an optimal set of calves.

问题简述:

  新建一个大学,给N(奇数)个学生提供助学金,该学校最多能提供助学金数额为F。总共有C个学生,同时给出学生成绩与助学金,学校希望这个N个学生的成绩的中位数尽可能地大,求这个中位数的最大值。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=1e5+10;
pair<int ,int >a[N];
int up[N],low[N]; 

//一定要选n头,尽可能使得中位数更大。 
int n,c; 

//策略, 
long long f; 

int main()
{
    cin>>n>>c>>f;
    for(int i=0;i<c;i++)cin>>a[i].first>>a[i].second; 
    sort(a,a+c);
    
    long long sum=0;
    priority_queue<int> q,p;
    
    for(int i=0;i<n/2;i++ )
    {
    	sum+=a[i].second;
    	q.push(a[i].second);
	}
	
	for(int i=n/2;i<c-n/2;i++)
	{
		low[i]=sum;
		if(a[i].second<q.top()&&!q.empty())
		{
			sum-=q.top();
			q.pop();
			sum+=a[i].second;
			q.push(a[i].second);
		}
	}
    
    sum=0;
	
	for(int i=c-1;i>=c-n/2;i--)
    {
    	sum+=a[i].second;
    	p.push(a[i].second);
	}
	
	for(int i=c-n/2-1;i>=n/2;i--)
	{
		up[i]=sum;
		if(a[i].second<p.top()&&!p.empty())
		{
			sum-=p.top();
			p.pop();
			sum+=a[i].second;
			p.push(a[i].second);
		}
	}
    int ans=-1;
    for(int i=c-n/2-1;i>=n/2;i--)
    {
    	if(up[i]+low[i]+a[i].second<=f)
    	{
    		ans=a[i].first;
    		break;
		}
	}
   cout<<ans<<endl;


return 0;
}

 1.做这题非常痛苦,想不到有什么好的方法能在如此大的数据范围内穷尽所有情况,看了题解后才知道,其实不一定要穷举所有情况,毕竟要求的是中位数,而且越大越好,那我们就不妨假设这个数为中为数时,能不能满足条件,即从小到大排序后,找出满足条件的最大的第一个数即可。

2.确立了假设的中位数字之后,问题就简单了,对于中位数的左右,只需要储存最小花费就行,毕竟,成绩大小已经没有关系,这里是用up,low数组,一个是从前往后计算,一个从后往前计算,计算过程中,需要更新优先队列--因为当前值如果比队列最大的元素小,那么就可以舍弃最大元素,而选择这个元素了(前提队列是非空的)。

3.首元素为0时,中位数范围是2/n---c-2/n-1.

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值