Poj 2010 Moo University - Financial Aid【优先队列+神技巧】

14 篇文章 0 订阅

Moo University - Financial Aid
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 7075 Accepted: 2053

Description

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. 

Input

* Line 1: Three space-separated integers N, C, and F 

* Lines 2..C+1: Two space-separated integers per line. The first is the calf's CSAT score; the second integer is the required amount of financial aid the calf needs 

Output

* Line 1: A single integer, the maximum median score that Bessie can achieve. If there is insufficient money to admit N calves,output -1. 

Sample Input

3 5 70
30 25
50 21
20 20
5 18
35 30

Sample Output

35

Hint

Sample output:If Bessie accepts the calves with CSAT scores of 5, 35, and 50, the median is 35. The total financial aid required is 18 + 30 + 21 = 69 <= 70. 

Source


题意+题解 点这里


想到怎么简单粗暴的处理了,但是个人难以实现,还是借鉴了大神们的思路,看了之后还是不太懂.....


个人想到的解法就是:(暴力模拟)

1,按需要的经费升序排列,把前n 个直接选中,记录此时的中位数和总费用,

2,对于后面的元素,如果数字不大于中位数,那么不用处理,因为计算替换掉某一个值,中位数也不可能变大

3,对于大于中位数的,抛弃选中的物品中小于这个数而且价值最高的物品

(新加入的物品费用肯定比当前选中的任何一个物品的价值高,而且如果把某一物品替换后,中位数一定会增大,那么就选择其中数字没有自身大,而且费用最高的那个物品,这样更省钱)


思路大概是这样,进行模拟的话,情况比较复杂,个人没能实现.....


某个大神的分析:


在保证中位数最大的情况下,如何保证正好取N个!
    DP的话数据量太大,搜索也搜不动,贪心很难保证恰好取N个。


    根据 sorce递增排序, 枚举中位数a[i]! 那么肯定在 [1,i-1]区间内取N/2个数, 在 [i+1,C]内取N/2个数!


    问题就转化为: 求分别求区间 [1,i-1]、[i+1,C]中N/2个数的最小值。  N/2+1<=i<=C-N/2
    这时需要一个高效的数据结构!
    我们来维护一个 大小为 N/2的最大堆。
    例如对于区间 [1,i-1],  初始堆为 [1,N/2], sum= sum([1,N/2].aid)  (aid为所需资助)
    每次拓展区间长度时,我们只需比较新扩展的结点(key)与 堆顶元素(fro)的大小。 if (key<fro) 则弹出堆顶元素,并且使 key入堆,并更新 sum。(保证和最小!)
  区间[i+1,C]也是一样。
    最后倒序枚举中位数即可。

/*
http://blog.csdn.net/liuke19950717
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=100005;
const int inf=0x3f3f3f3f;
struct node
{
	int num,val;
}x[maxn];
int cmp(node a,node b)
{
	if(a.num==b.num)
	{
		return a.val<b.val;
	} 
	return a.num<b.num;
}
int lower[maxn],upper[maxn];
int slove(int n,int m,int s)
{
	memset(lower,-1,sizeof(lower));
	memset(upper,-1,sizeof(upper));
	sort(x,x+n,cmp);
	int mid=m/2,cnta=0,cntb=0;
	priority_queue<int> qa;
	priority_queue<int> qb;
	for(int i=0;i<n;++i)
	{
		lower[i]=(qa.size()==mid)?cnta:inf;
		upper[n-i-1]=(qb.size()==mid)?cntb:inf;
		qa.push(x[i].val);
		qb.push(x[n-i-1].val);
		cnta+=x[i].val;
		cntb+=x[n-i-1].val;
		if(qa.size()>mid)
		{
			cnta-=qa.top();
			qa.pop();
		}
		if(qb.size()>mid)
		{
			cntb-=qb.top();
			qb.pop();
		}
	}
	for(int i=n-1;i>=0;--i)
	{
		if(lower[i]+x[i].val<=s-upper[i])
		{
			return x[i].num;
		}
	}
	return -1;
}
int main()
{
	int n,m,s;
	//freopen("shuju.txt","r",stdin);
	while(~scanf("%d%d%d",&m,&n,&s))
	{
		for(int i=0;i<n;++i)
		{
			scanf("%d%d",&x[i].num,&x[i].val);
		}
		printf("%d\n",slove(n,m,s));	
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值