POJ-2010 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.


题解

题意
给定长度为 M M M的数组,从中选择 N N N个数,每个数有花费,要在总花费不超过 K K K​的情况下,求出最大中位数

分析

考虑当前中位数为 x x x​​ 位置上的数,那么得找到 ⌊ N / 2 ⌋ \lfloor N/2 \rfloor N/2​个小于 x x x​ 的数和大于 x x x​ 的数,且总花费不超过 K K K

此时,我们将原数组按数值排序,假设从小到大排序

那么上面的问题能转化为,左边选 ⌊ N / 2 ⌋ \lfloor N/2 \rfloor N/2个数,右边选 ⌊ N / 2 ⌋ \lfloor N/2 \rfloor N/2个数,且花费不超过 K K K

继续考虑,左边个数确定,那么总花费要小肯定得选前 ⌊ N / 2 ⌋ \lfloor N/2 \rfloor N/2个低花费的数,

问题得解,就是对于位置 x x x ,得维护左右两边前 ⌊ N / 2 ⌋ \lfloor N/2 \rfloor N/2个小的数,如果总花费不超过 K K K​ 更新答案

对于左边右边,用合并堆维护信息,预处理所有左边所有情况,右边所有情况

最后 O ( n ) O(n) O(n) 扫一遍,更新答案即可

//322971B
/*
  @Author: YooQ
*/
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define sc scanf
#define pr printf
#define ll long long
#define int long long
#define FILE_OUT freopen("out", "w", stdout);
#define FILE_IN freopen("in", "r", stdin);
#define debug(x) cout << #x << ": " << x << "\n";
#define AC 0
#define WA 1
#define INF 0x3f3f3f3f
const ll MAX_N = 2e6+5;
const ll MOD = 1e9+7;
int N, M, K;
int arr[MAX_N];
int brr[MAX_N];

int id[MAX_N];
bool cmp(int x, int y) {
	return arr[x] < arr[y];
}

struct Leftist {
	struct Tr {
		int k, l, r, dis, p;
	}tr[MAX_N];
	
	int indx;
	int sz;
	int root;
	int price;
	
	int mk(int x) {
		++indx;
		tr[indx].k = x;
		return indx;
	}
	
	int merge(int x, int y) {
		if (!x || !y) return x | y;
		if (tr[x].k < tr[y].k) {
			swap(x, y);
		}
		tr[x].r = merge(tr[x].r, y);
		tr[tr[x].r].p = x;
		if (tr[tr[x].r].dis > tr[tr[x].l].dis) {
			swap(tr[x].l, tr[x].r);
		}
		tr[x].dis = tr[tr[x].r].dis + 1;
		return x;
	}
	
	int insert(int x) {
		root = merge(root, mk(x));
		++sz;
		return root;
	}
	
	void remove(int rt) {
		if (!rt) return;
		--sz;
		int raw = rt;
		int p = tr[rt].p;
		rt = merge(tr[rt].l, tr[rt].r);
		tr[rt].p = p;
		raw == tr[p].l ? tr[p].l = rt : tr[p].r = rt;
		
		while (p) {
			if (tr[tr[p].r].dis > tr[tr[p].l].dis) {
				swap(tr[p].l, tr[p].r);
			}
			if (tr[p].dis == tr[tr[p].r].dis + 1) break;
			tr[p].dis = tr[tr[p].r].dis + 1;
			p = tr[p].p;
		}
	}
	
	void pop() {
		root = merge(tr[root].l, tr[root].r);
		tr[root].dis = tr[tr[root].r].dis + 1;
		--sz;
	}
	
	int top() {
		return tr[root].k;
	}
	
}L, R;

int preL[MAX_N];
int preR[MAX_N];

void solve(){
	sc("%lld%lld%lld", &N, &M, &K);
	
	for (int i = 1; i <= M; ++i) {
		sc("%lld%lld", &arr[i], &brr[i]);
		id[i] = i;
	}
	sort(id+1, id+1+M, cmp);
	int limit = ((N-1)>>1);
	for (int i = 1; i <= M; ++i) {
		if (i <= limit) {
			L.insert(brr[id[i]]);
			L.price += brr[id[i]];
		} else {
			if (brr[id[i]] < L.top()) {
				L.price -= L.top();
				L.pop();
				L.insert(brr[id[i]]);
				L.price += brr[id[i]];
			}
		}
		preL[i] = L.price;
	}
	
	for (int i = M; i; --i) {
		if (i + limit > M) {
			R.insert(brr[id[i]]);
			R.price += brr[id[i]];
		} else {
			if (brr[id[i]] < R.top()) {
				R.price -= R.top();
				R.pop();
				R.insert(brr[id[i]]);
				R.price += brr[id[i]];
			}
		}
		preR[i] = R.price;
	}
	
	int ans = -1;
	for (int i = M - limit; i > limit; --i) {
		if (preL[i-1] + preR[i+1] + brr[id[i]] <= K) {
			pr("%lld\n", arr[id[i]]);
			return;
		}
	}
	puts("-1");
}

signed main()
{
	#ifndef ONLINE_JUDGE
	//FILE_IN
	FILE_OUT
	#endif
	int T = 1;//cin >> T;
	while (T--) solve();

	return AC;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hexrt

客官,请不要给我小费!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值