优先队列 POJ2010

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. 

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

代码:

#include<iostream>
#include<cstdio>
#include<algorithm> 
#include<queue>
#include<string.h>	//memset函数 
using namespace std;

struct node{
	int a,b;
	bool operator<(const node &oth)const{
		return b<oth.b;	//对b排序,每次取出最大 
	}
}c[100005];

bool cmp(node x,node y){
	return x.a<y.a;
}

int main(){
	int n,m,f,i,j;
	__int64 left[100005],right[100005],ans=-1;
	memset(left,0,sizeof(left));
	memset(right,0,sizeof(right));
	cin>>n>>m>>f;
	priority_queue <node> dui;
	for(i=0;i<m;i++) cin>>c[i].a>>c[i].b;
	sort(c,c+m,cmp);
	
	for(i=0;i<n/2;i++){	//初始化左侧最小值 
		left[n/2]+=c[i].b;
		node t;
		t.a=c[i].a;
		t.b=c[i].b;
		dui.push(t);
	}
	for(i=n/2+1;i<m-1-n/2+1;i++){	//求左侧所有最小值 
		node t=dui.top();
		if(c[i-1].b<t.b){
			left[i]=left[i-1]-t.b+c[i-1].b;
			dui.pop();
			node t;
			t.a=c[i-1].a;
			t.b=c[i-1].b;
			dui.push(t);
		}
		else{
			left[i]=left[i-1];
		}
	}
	while(!dui.empty()){	//清空队列 
		dui.pop();
	}
	for(i=m-1;i>m-1-n/2;i--){	//初始化右侧最小值 
		right[m-1-n/2]+=c[i].b;
		node t;
		t.a=c[i].a;
		t.b=c[i].b;
		dui.push(t);
	}
	for(i=m-1-n/2-1;i>n/2-1;i--){	//求右侧所有最小值 
		node t=dui.top();
		if(c[i+1].b<t.b){
			right[i]=right[i+1]-t.b+c[i+1].b;
			dui.pop();
			node t;
			t.a=c[i+1].a;
			t.b=c[i+1].b;
			dui.push(t);
		}
		else{
			right[i]=right[i+1];
		}
	}
	for(i=m-1-n/2;i>n/2-1;i--){	
		if(c[i].b+left[i]+right[i]<=f){	//符合条件,输出答案 
			ans=c[i].a;
			break;
		}
	}
	printf("%d",ans);
}
基本思路:对分数排序后,先求出中位数所有可能情况下,左右两侧分别存在的最小值的和(用优先队列实现排序,每次取出最小值做比较,然后从后向前找出第一个符合条件的

猜你喜欢

转载自blog.csdn.net/littlewhitelv/article/details/79922200
今日推荐