POJ - 2010 Moo University - Financial Aid(二分)

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

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. 

题意:给你c个点,每个点有两个属性,一个是成为成绩g,一个是资金f,又给你总资金F,然后让你从这c个点中选出n个,这n个点满足两点:1,n个点的资金和不大于总资金F,2:n个点成绩的中位数尽可能的大。

二分思路:
直接二分中位数(当然是所给数据的下表),然后如果能找出N / 2多个小于中位数的,还能找出N / 2多个大于中位数的,证明满足条件,然后保存数据,下界向上移动,当然得到这些数的同时一定要对F进行判断,否则懵逼
思维:对于可以用二分的,我们直接对症下药,要求什么,我们先试着看能否二分这个所求结果,如果无果再想其他办法
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;


#define pb push_back
#define mp make_pair
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r


typedef long long LL;
typedef pair<int, int > PII;
typedef unsigned long long uLL;

template<typename T>
void print(T* p, T* q, string Gap = " ") {
	int d = p < q ? 1 : -1;
	while(p != q) {
		cout << *p;
		p += d;
		if(p != q) cout << Gap;
	}
	cout << endl;
}
template<typename T>
void print(const T &a, string bes = "") {
	int len = bes.length();
	if(len >= 2)cout << bes[0] << a << bes[1] << endl;
	else cout << a << endl;
}

const int INF = 0x3f3f3f3f;
const int MAXM = 2e5;
const int MAXN = 1e5 + 5;
int N, C, F;
struct o {
	int g, f;
	int id;
} OL[MAXN], OR[MAXN];

bool cmp1(const o &p, const o &q) {
	return p.f < q.f;
}

bool cmp2 (const o &p, const o &q) {
	return p.g < q.g;
}

int CA(int mid) {
	int sum = OL[mid].f;
	int l = 0, r = 0;
	int Z = N / 2;
	for(int i = 0; i < C; i ++) {
		if(l < Z && OR[i].id < mid && sum + OR[i].f <= F) {
			sum += OR[i].f;
			l ++;
		}
		if(r < Z && OR[i].id > mid && sum + OR[i].f <= F) {
			sum += OR[i].f;
			r ++;
		}
	}
	if(l < Z && r < Z) {
		return -1;
	} else if(l < Z) {
		return 1;
	} else if(r < Z) {
		return 2;
	} else {
		return 3;
	}
}
int main() {
	while(~scanf("%d%d%d", &N, &C, &F)) {
		LL Max = 0;
		for(int i = 0; i < C; i ++) {
			scanf("%d%d", &OL[i].g, &OL[i].f);
		}
		sort(OL, OL + C, cmp2);
		for(int i = 0; i < C; i ++) {
			OL[i].id = i;
			OR[i].f = OL[i].f;
			OR[i].g = OL[i].g;
			OR[i].id = i;
		}
		sort(OR, OR + C, cmp1);
		int lb = -1, ub = C - 1;
		int ret = -1;
		while(ub - lb > 1) {
			int mid = (ub + lb) >> 1;
			int Z = CA(mid);
			if(Z == 1) {
				lb = mid;
			} else if(Z == 2) {
				ub = mid;
			} else if(Z == -1) {
				ret = -1;
				break;
			} else {
				ret = OL[mid].g;
				lb = mid;
			}
		}
		printf("%d\n", ret);
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值