POJ_2010

POJ 2010

题目

​ C头牛入学,第i头牛入学成绩为 s c o r e i score_i scorei,需要 a i d i aid_i aidi的助学补助。学校最多批准F元助学补助,且只招N头牛,求招的牛的分数中位数最大是多少。

标签

​ 优先队列

输入

​ 第一行输入三个整数N C F,N为奇数。

​ 之后的n行中,每行输入使用空格分隔的两个整数 s c o r e i , a i d i score_i, aid_i scorei,aidi

输出

​ 一行,输出招的N头牛成绩中位数的最大可能值。

思路

​ 首先将所有牛按照成绩进行排序,然后枚举每一头牛作为中位数,左右取N/2头牛,判断总补助是否少于F。

​ 为做到这点,需要维护每头牛左/右补助最低的N/2头牛的总补助。可以使用优先队列,存储前N/2小的补助。

注意

  • 成绩最高/低的N/2牛不可能作为中位数,因此在优先队列size没达到N/2时,将cost设为INF

代码

#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5+7;
const int INF = 0x3f3f3f3f;

struct Cow {
	int score;
	int aid;
	
	bool operator<(const Cow c) {
		return score < c.score;
	}
};

Cow c[N];
ll lcost[N], rcost[N];
priority_queue<int> q;
priority_queue<int> qr;

int main() {
	ll N, C, F;
	cin >> N >> C >> F;
	for (int i = 0; i < C; ++i) {
		cin >> c[i].score >> c[i].aid;
	}
	sort (c, c+C);
	int half = N/2;
	ll sl = 0, sr = 0;
	for (int i = 0; i < C; ++i) {
		lcost[i] = (q.size() == half) ? sl : INF;
		
		q.push(c[i].aid);
		
		sl += c[i].aid;
		if (q.size() > half) {
			sl -= q.top();
			q.pop();
		}
	}
	
	for (int i = C-1; i >= 0; --i) {
		rcost[i] = (qr.size() == half) ? sr : INF;
		qr.push(c[i].aid);
		sr += c[i].aid;
		if (qr.size() > half) {
			sr -= qr.top();
			qr.pop();
		}
	}
	for (int i = C-1; i >= 0; --i) {
		if (lcost[i] + rcost[i] + c[i].aid <= F) {
			cout << c[i].score << endl;
			return 0;
		}
	}
	cout << -1 << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值