POJ 2010 Moo University - Financial Aid 优先队列 好题!

题目链接

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.

题目大意:
有一所奶牛大学要招生,准备招N个(N为奇数),共有C个报名,每头奶牛有一个分数和它所需要的学费补助,目前最多能提供总数为F的补助,求出满足条件下最大的得分中位数。如果F费用没办法招满N头牛,那么输出-1,否则输出最大的得分中位数。

解题思路:
将奶牛按照分数排序,经过初步观察,可以发现每头奶牛都有成为中位数的可能(当然如果N>1,两边的边界处会有一些奶牛无法成为中位数),那么我们可以分别计算出每头牛左侧 (N-1)/2头牛的最小学费补助和,以及每头牛右侧 (N-1)/2头牛的最小学费补助和。
最后从大到小遍历这N头牛,如果总的补助满足条件,那么它就是答案。
可借助优先队列完成更新最小学费补助和的操作。

碎碎念:起初我还想在对有序的奶牛一次遍历的过程中就计算出左右两侧的最小学费和,这样的话满足条件便可提前结束,不过这样做右侧的更新较为简单,但左侧的更新就很困难,有一些学费补助相等的情况下比较难处理,最终还是老老实实全部计算出,然后再找寻答案了。。。而且别忘了无法满足招生条件时要输出-1

#include<iostream>
#include<stdio.h>
#include<queue>
#include<algorithm>
using namespace std;
struct node {
	int v, cost;
}list[100001];
//low[i]表示排序之后的牛i作中位数时,前i-1头牛中选出(n-1)/2头牛的最小补助
//hight[i]表示排序之后的牛i作中位数时,除去前i头牛之后,从剩余的牛中选出(n-1)/2头牛的最小补助
int low[100001]; int hight[100001];
priority_queue<int>que1;
priority_queue<int>que2;
#define INF 0x3fffffff
bool cmp(node a, node b) { return a.v < b.v; }//按价值排序
int main() {
	int n, c, f;
	scanf("%d%d%d", &n, &c, &f);
	for (int i = 1; i <= c; i++) {
		scanf("%d%d", &list[i].v, &list[i].cost);
	}
	sort(list + 1, list + 1 + c, cmp);

	int mid = (n - 1) / 2;
	int total = 0;
	for (int i = 1; i <= c; i++) {
		if (que1.size() < mid) {
			low[i] = INF;
		}
		else low[i] = total;
		total += list[i].cost;
		que1.push(list[i].cost);
		if (que1.size() > mid) {//如果队列过多,就把堆顶也就是钱数最大的奶牛去掉
			total -= que1.top();
			que1.pop();
		}
	}
	total = 0;//重新清零
	for (int i = c; i >0; i--) {
		if (que2.size() < mid) {
			hight[i] = INF;
		}
		else hight[i] = total;
		total += list[i].cost;
		que2.push(list[i].cost);
		if (que2.size() > mid) {//如果队列过多,就把堆顶也就是钱数最大的奶牛去掉
			total -= que2.top();
			que2.pop();
		}
	}
	bool flag = false;
	int j;
	for (j = c; j > 0; j--) {
		if (low[j] != INF&&hight[j] != INF&&low[j] + hight[j] + list[j].cost <= f) {
			flag = true;
			break;
		}
	}
	if (flag) { cout << list[j].v << endl; }
	else cout << -1 << endl;
	//system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值