ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

POJ-2010 Moo University - Financial Aid

2021-08-04 23:29:37  阅读:307  来源: 互联网

标签:Financial int University tr brr Moo root id dis


文章目录

题面

传送门

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;
}

标签:Financial,int,University,tr,brr,Moo,root,id,dis
来源: https://blog.csdn.net/qq_45754027/article/details/119395292

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有