Poj2010(堆-優先隊列)

                                     Moo University - Financial Aid

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 14489   Accepted: 4304

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.

Source

USACO 2004 March Green

 

題意:奶牛學校招生,c頭奶牛報名,要選n頭(n爲奇數),學校是義務制,所以每頭奶牛的學費都由學校負責。每頭奶牛都由自己的考試分數和它需要花的學費,學校總共有f的資金,問合法招生方案中中間分數(即排名第(n+1)/2)最高的是多少。

題解:先將所有的奶牛按照分數由高到低排序,假設k是招的奶牛中排名中間的那頭,按照排序可知,[1,k-1]中的奶牛必定被招了(n-1)/2頭,[k+1,c]中也必定被招了(n-1)/2頭,而且無論招的是誰,分數是怎麼樣,最後影響結果的都只是k的分數。於是,可以預處理dpl[i]代表[1,i]頭牛中選出(n-1)/2頭牛的最小花費,dpr[i]代表[i,c]頭牛中選出(n-1)/2頭牛的花費,預處理方法可以用一個大頂堆,複雜度nlogn,最後枚舉中間牛複雜度n。

 

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<cstring>
#define ll long long
using namespace std;
int n,c;
int dpl[101000],dpr[101000];
ll f;
priority_queue<ll> Q;
struct Cow {
	ll at;
	ll aid;

	bool operator <(Cow &o)const {
		return at<o.at;
	}
} cow[101000];

int main() {
	while(scanf("%d%d%lld",&n,&c,&f)!=EOF) {
		for(int i=1; i<=c; i++) {
			scanf("%lld%lld",&cow[i].at,&cow[i].aid);
		}
		sort(cow+1,cow+c+1);
		int low=(n+1)/2;
		memset(dpl,0,sizeof(dpl));
		memset(dpr,0,sizeof(dpr));
		while(!Q.empty())
			Q.pop();
		for(int i=1; i<low; i++) {
			Q.push(cow[i].aid);
			dpl[low]+=cow[i].aid;
		}
		for(int i=low+1; i<=c; i++) {
			if(cow[i-1].aid>=Q.top()) {
				dpl[i]=dpl[i-1];
			} else {
				dpl[i]=dpl[i-1]+cow[i-1].aid-Q.top();
				Q.pop();
				Q.push(cow[i-1].aid);
			}
		}
		while(!Q.empty())
			Q.pop();
		for(int i=c; i>c-low+1; i--) {
			Q.push(cow[i].aid);
			dpr[c-low+1]+=cow[i].aid;
		}
		for(int i=c-low; i>=1; i--) {
			if(cow[i+1].aid>=Q.top()) {
				dpr[i]=dpr[i+1];
			} else {
				dpr[i]=dpr[i+1]+cow[i+1].aid-Q.top();
				Q.pop();
				Q.push(cow[i+1].aid);
			}
		}
		ll ans=-1;
		for(int i=low; i<=c-low+1; i++) {
			if(dpl[i]+dpr[i]+cow[i].aid<=f) {
				ans=max(cow[i].at,ans);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章