POJ2010

Moo University -Financial Aid

Description

Bessie noted thatalthough humans have many universities they can attend, cows have none. Toremedy this problem, she and her fellow cows formed a new university called TheUniversity of Wisconsin-Farmside,"Moo U" for short.

Not wishing toadmit dumber-than-average cows, the founders created an incredibly preciseadmission exam called the Cow Scholastic Aptitude Test (CSAT) that yieldsscores in the range 1..2,000,000,000.

Moo U is veryexpensive to attend; not all calves can afford it.In fact, most calves needsome sort of financial aid (0 <= aid <=100,000). The government does notprovide scholarships to calves,so all the money must come from the university'slimited fund (whose total money is F, 0 <= F <= 2,000,000,000).

Worse still, Moo Uonly 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 Ncalves in order to maximize educational opportunity. She still wants the medianCSAT score of the admitted calves to be as high as possible.

Recall that themedian of a set of integers whose size is odd is the middle value when they aresorted. For example, the median of the set {3, 8, 9, 7, 5} is 7, as there areexactly two values above 7 and exactly two values below it.

Given the scoreand required financial aid for each calf that applies, the total number ofcalves to accept, and the total amount of money Bessie has for financial aid,determine the maximum median score Bessie can obtain by carefully admitting anoptimal set of calves.

Input

* Line 1: Threespace-separated integers N, C, and F

* Lines 2..C+1:Two space-separated integers per line. The first is the calf's CSAT score; thesecond integer is the required amount of financial aid the calf needs

Output

* Line 1: A singleinteger, the maximum median score that Bessie can achieve. If there isinsufficient 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

 

 

贝西注意到,尽管人类有许多大学可以参加,但牛却没有。为了解决这个问题,她和她的同伴们组成了一所新的大学,名为威斯康星大学,简称“Moo U”。

为了不让奶牛比普通的奶牛多,创始人创建了一个非常精确的入学考试,叫做“奶牛学术能力测试”(CSAT),它的得分范围1...2,000,000,000。

参加Moo U是非常昂贵的;并不是所有的犊牛都能负担得起。事实上,大多数小牛需要某种形式的经济援助(0 < =援助< = 100,000)。政府不提供给小牛的奖学金,所以所有的钱都必须来自大学的有限基金(其总金额为F,0 <= F <= 20亿)。

更糟糕的是,Moo U只有一个教室可容纳N只小牛(1 <= N <= 19,999;N为奇数)。贝茜想要录取N只小牛,以最大限度地利用教育机会。她仍然想要小牛们得分的中位数尽可能高。

回想一下,在排序时,其大小为奇数的整数集的中值是中间值。例如,集合{3、8、9、7、5}的中位数是7,因为正好有两个值比7大,有两个值比7小。

考虑到每只小牛的评分和所需的经济援助,接受的犊牛总数,以及贝西为经济援助所提供的总金额,贝茜就可以确定一组最优的幼崽,它们的得分中位数最大。

 

 

 

 

*第1行:三个空格分隔的整数N、C和F

*第2行:C+1:每行两个空格分隔的整数。第一个是小牛的CSAT分数;第二个整数是小牛所需的经济援助金额

输出

一行1:一个整数,贝西可以达到的最大值。如果没有足够的钱来接收牛,输出- 1

 

0

1

2

3

4

分数

30

50

20

5

35

费用

25

21

20

18

30

 

 

 

0

1

2

3

4

分数

50

35

30

20

5

费用

21

30

25

20

18

  

 

 

 

 

 排序后

 

 

 

 

 

0

1

2

50

35

5

21

30

18

 

 

 堆                                            

 

 

 

 

 

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
using namespace std;

typedef struct{
	int score;
	int aid;
}moo;

int C,N,F;

moo cow[100000];
moo heap[19999];

int judge(){
	int sum = 0;
	for(int i = 0;i < C;i++){
		sum += heap[i].aid;	
	}
	if(sum > F){
		return 0;
	}else{
		return 1;
	}
}

int compare(moo a,moo b){
	if(a.score>b.score){
		return 1;
	}else{
		return 0;
	}
}
int compare2(moo a,moo b){
	if(a.aid>b.aid){
		return 0;
	}else{
		return 1;
	}
}

int main(){
	//输入 
	while(cin>>C>>N>>F){
		for(int i = 0;i < N;i++){
			cin>>cow[i].score;
			cin>>cow[i].aid;
		}
		//排序
		sort(cow,cow+N,compare); 
		
		int point = C/2;                         //point指向第一个可能的中位数         
		do{	
			memset(heap,99,sizeof(moo)*C);
			heap[C/2] = cow[point];                //heap数组 下标C/2的位置 固定存放中位数
			for(int i = 0;i < point;i++){                     //遍历cow[point]左边全部值 找到最小的C/2个 
				make_heap(heap,heap+C/2,compare2);           //将heap[C/2]以左作为一个小根堆 
				if(heap[0].aid > cow[i].aid){                //堆顶heap[0]的值大于cow[i]就将其替换 
					heap[0] = cow[i];
				}
				
			}
			for(int i = point+1;i < N;i++){                   //遍历cow[point]又边全部值 找到最小的C/2个 
				make_heap(heap+(C+1)/2,heap+C,compare2);     //将heap[C/2]以右作为另一个小根堆 
				if(heap[(C+1)/2].aid > cow[i].aid){         //堆顶heap[(C+1)/2]的值大于cow[i]就将其替换
					heap[(C+1)/2] = cow[i];
				}
			}
			point++;
			if(judge() == 1||point == N-C/2){
					break;
			}	
		}while(judge() == 0);
			
		if(judge() == 1){
			cout<
      
      
     
     
    
    
   
   

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值